HDU 3068 最長回文

Manacher算法練筆,O(n)求最長回文子串。

參考資料:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824

http://www.felix021.com/blog/read.php?2040

?

后綴數組和拓展KMP也可以求,不過時間復雜度都是O(nlogn)。

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 const int MAXN = 110010;
 5 
 6 char s[MAXN];
 7 char str[ MAXN << 1 ];
 8 int p[ MAXN << 1 ];
 9 int newL, n;
10 
11 int min( int a, int b )
12 {
13     return a < b ? a : b;
14 }
15 
16 int max( int a, int b )
17 {
18     return a > b ? a : b;
19 }
20 
21 void init()
22 {
23     int i;
24     str[0] = '?';
25     str[1] = '#';
26     newL = 2;
27     for ( i = 1; s[i]; ++i )
28     {
29         str[ newL++ ] = s[i];
30         str[ newL++ ] = '#';
31     }
32     str[ newL ] = '\0';
33     n = newL;
34 
35     return;
36 }
37 
38 void DP()
39 {
40     int maxID = 0, id;
41     for ( int i = 1; i < newL; ++i )
42     {
43         if ( maxID > i ) p[i] = min( p[ (id << 1) - i ], p[id] + id - i );
44         else p[i] = 1;
45 
46         while ( str[ i + p[i] ] == str[ i - p[i] ] ) ++p[i];
47         if ( p[i] + i > maxID )
48         {
49             maxID = p[i] + i;
50             id = i;
51         }
52     }
53     return;
54 }
55 
56 int main()
57 {
58     while ( ~scanf( "%s", &s[1] ) )
59     {
60         init();
61         DP();
62 
63         int maxL = 0;
64         for ( int i = 1; i < n; ++i )
65             maxL = max( maxL, p[i] - 1 );
66         printf( "%d\n", maxL );
67     }
68     return 0;
69 }

?

轉載于:https://www.cnblogs.com/GBRgbr/archive/2013/05/30/3107751.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274223.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274223.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274223.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ux設計師薪水_客戶現在也是UX設計師

ux設計師薪水Some of you probably know by now, I’m not too fond of the monster the UX industry has become. It’s overblown, overcomplicated and often dishonest towards the clients. It’s also in itself undefined. (where is the E in Experience?)你們中的某些…

說說godaddy

俗稱狗他爹&#xff0c;是全世界最大的一級域名注冊和服務商&#xff0c;中國只有國家是一級&#xff0c;萬網等都是2級&#xff0c;如果你的域名是在萬網注冊的&#xff0c;那你就out啦&#xff0c;因為有諸多使用的限制&#xff0c;比如我之前為了試試萬網的域名&#xff0c;…

分步表單_角色創建分步指南

分步表單The first thing most of us designers are taught is the concept of personas and the necessity of them when it comes to UX and product design. However, knowing is different from applying and it can be difficult to know where to begin when we’re aske…

svg配合css3動畫_帶有Adobe Illustrator,HTML和CSS的任何網站的SVG動畫

svg配合css3動畫A top trend in web design for 2020 is the increased use of SVG animations on web pages and in logo design. In this article, we will implement a simple and straight forward method to create relatively complex animation. We will use Adobe Illu…

【原創-長文】openstack 版本D安裝配置及本次安裝中遇到的問題

openstack配置 一、硬件及操作系統要求 硬件&#xff1a;IBM服務器R410 兩臺、網線、顯示器、鍵盤若干&#xff0c;100M光纖&#xff08;硬性要求&#xff09; 操作系統&#xff1a;兩臺服務器均安裝Ubuntu server 12.04 LTS 二、安裝步驟&#xff08;server-1與server-2公共部…

基于pt100溫度計仿真_基于8pt網格的設計系統

基于pt100溫度計仿真重點 (Top highlight)This article is the 2nd in a two part series — to the previous chapter in which I demonstrate how to establish an 8pt grid.本文是該系列文章的第二部分 &#xff0c;這是上一章 的第二部分 &#xff0c;在上一章中&#xff0…

GL ERROR - after deleteUnusedTextures() glError (0x502)

最近用百度提供的javascript API開發地圖時&#xff0c;html頁面在手機瀏覽器中拖動地圖時會出現GL ERROR - after deleteUnusedTextures() glError (0x502)的異常&#xff0c;看了下國外論壇異常的說法&#xff0c;經調試&#xff0c;找出解決辦法&#xff0c;異常原因還是和布…

利用 k8s 建立軟件商店_為企業建立應用商店

利用 k8s 建立軟件商店It’s June 2019. I’m sitting in a conference room in Research Triangle Park in North Carolina. At the end of the table are the two executives that have been tapped to lead a new endeavor on behalf of IBM’s $34 billion acquisition of …

[轉]gcc生成動態庫靜態庫

http://blog.csdn.net/hzn407487204/article/details/5323254轉載于:https://www.cnblogs.com/hengli/archive/2013/06/07/3125354.html

蘋果復興_類型復興的故事:來自Type West的經驗教訓

蘋果復興Last Fall, I began the 去年秋天&#xff0c;我開始 在舊金山的Type West program at the Letterform檔案庫中Letterform Archive in San Francisco. For those of you who don’t know, the Letterform Archive is creative heaven — a type nerd’s letter art co…

C#調用ATL COM

作者&#xff1a;朱金燦 來源&#xff1a;http://blog.csdn.net/clever101 簡單介紹C#程序如何調用ATL編寫的COM組件。 首先新建一個ATL工程&#xff0c;具體如下&#xff1a; 1. 填寫工程名稱和路徑&#xff0c;如下圖&#xff1a; 2. 選擇工程的服務器類型為動態鏈接庫&a…

浪潮世科和浪潮軟件什么關系_社交圖形浪潮

浪潮世科和浪潮軟件什么關系Nowadays, the cornucopia of graphics seems like a given. However, it was not so long ago that infographics were scarce and lived in closed ecosystems. The majority of graphics were published in newspapers, magazines, or books, and…

PHP圖形圖像的典型應用 --常用圖像的應用(驗證碼)

php生成動態的驗證碼&#xff0c;是php防止惡意登陸或者注冊等常規手段-廢話不多說&#xff0c;直接看例子。&#xff08;只是一個簡單的應用&#xff0c;如果要安全或者更復雜的&#xff0c;請期待我以后的文章&#xff09; PHP生成驗證碼核心文件 (checks.php): <?php/*成…

接口練習

前臺代碼&#xff1a; <form id"form1" runat"server"> <div> 見面時間:<asp:TextBox ID"MeetTime" runat"server"></asp:TextBox><br /> 見面地點:<asp:TextBox ID"MeetAddr…

寫saas創業的書_我在SaaS創業公司擔任UX設計師的第一個月中學到的三件事

寫saas創業的書I recently transitioned from being a copywriter at an ad agency to a UX Designer at a SaaS startup. To add more multidisciplinary skills into the mix, I graduated with a Bachelor in Accountancy.我最近從一名廣告代理商的撰稿人過渡到了SaaS初創公…

ui項目答辯中學到了什么_我在UI設計9年中學到的12件事

ui項目答辯中學到了什么重點 (Top highlight)I know these can seem a bit clich but I will try to explain everything from my own experience.我知道這些內容似乎有些陳詞濫調&#xff0c;但我會嘗試根據自己的經驗來解釋所有內容。 第一名 (No.1 Never assume) The first…

linux下命令行的使用:使用sed命令操作文件

用該命令sed刪除文件test.txt中包含某個字符串abc的行: sed /adc/d test.txt >result.txt 在文件test.txt中刪除從開頭到含有某個字符串abc的行 sed 1,/abc/d test.txt >result.txt 獲取文件test.txt中包含字符串abc的行 cat test.txt |grep "abc" > resul…

ux的重要性_UX中清晰的重要性

ux的重要性重點 (Top highlight)Times, since the very first occurrences of web design in the 90’s, have changed a lot design-wise. The particular technology and its applications got more stable. Human-computer interaction (HCI) was deeply researched, design…

工欲善其事,必先利其器

vs2010中一些常用的快捷鍵、組合鍵&#xff1a; 1、快速格式化 CtrlED 2、注釋選中部分 CtrlEC 3、停止調試 ShiftF5 4、取消注釋選中部分 CtrlEU 5、顯示解決方案資源管理器 CtrlWS 6、快速折疊 CtrlMO 7、封裝一個字段 CtrlRE 8、查看屬性 CtrlWP 9…

可靠消息最終一致性設計_如何最終啟動您的設計產品組合

可靠消息最終一致性設計It’s not a secret that most designers procrastinate on their portfolios whether it is to update them or to create them in the first place.大多數設計師在更新產品組合時還是拖延產品組合并不是秘密。 首先創建它們 。 Hopefully, by the e…