ural 1297 Palindrome(Manacher模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

求最长回文子串。

http://acm.timus.ru/problem.aspx?space=1&num=1297

Manacher模板题,复杂度O(n),做这题纯属是为了验一下自己写的模板是否正确。

当然这题也可以用后缀数组来搞

 1 #include <iostream>
 2 #include <sstream>
 3 #include <ios>
 4 #include <iomanip>
 5 #include <functional>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <string>
 9 #include <list>
10 #include <queue>
11 #include <deque>
12 #include <stack>
13 #include <set>
14 #include <map>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cmath>
18 #include <cstring>
19 #include <climits>
20 #include <cctype>
21 using namespace std;
22 #define XINF INT_MAX
23 #define INF 0x3FFFFFFF
24 #define MP(X,Y) make_pair(X,Y)
25 #define PB(X) push_back(X)
26 #define REP(X,N) for(int X=0;X<N;X++)
27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
29 #define CLR(A,X) memset(A,X,sizeof(A))
30 #define IT iterator
31 typedef long long ll;
32 typedef pair<int,int> PII;
33 typedef vector<PII> VII;
34 typedef vector<int> VI;
35 #define MAXN 100010
36 char str[MAXN],s[MAXN];
37 int p[MAXN];
38 int n;
39 void Manacher(){
40     n=strlen(s);
41     str[0]=‘$‘;
42     str[1]=‘#‘;
43     for(int i=0;i<n;i++)str[2*i+2]=s[i],str[2*i+3]=‘#‘;
44     n=n*2+2;
45     str[n]=0;
46     int mx=0,id;
47     for(int i=1;i<n;i++){
48         if(mx>i)p[i]=min(p[2*id-i],mx-i);
49         else p[i]=1;
50         for(;str[i+p[i]]==str[i-p[i]];p[i]++);
51         if(p[i]+i>mx)mx=p[i]+i,id=i;
52     }
53 }
54
55 int main()
56 {
57     ios::sync_with_stdio(false);
58     while(scanf("%s",s)!=EOF){
59         Manacher();
60         int ans=1;
61         int id;
62         int maxx=1;
63         for(int i=0;i<n;i++){
64             if(p[i]>maxx)id=i,maxx=p[i];
65         }
66         //cout<<maxx<<endl;
67         p[id]--;
68         for(int i=id-p[id];i<=id+p[id];i++){
69             if(str[i]==‘#‘)continue;
70             printf("%c",str[i]);
71         }
72         puts("");
73     }
74     return 0;
75 }
时间: 2024-12-20 19:52:25

ural 1297 Palindrome(Manacher模板题)的相关文章

Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots»

URAL 1297. Palindrome(输出最长回文子串--后缀数组)

Input The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters. Output The longest substring with mentioned property. If ther

HDU 3068 最长回文(Manacher模板题)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24164    Accepted Submission(s): 8852 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组c

URAL 1297 Palindrome 后缀数组

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots Unlimited? has infiltrated into "U.S. Robotics".

URAL 1297. Palindrome(后缀数组求最大回文串)

题目大意:给你一串字符串,让你求出来它存在的最长连续的回文串. 解题思路:先把字符串逆序加到数组中,然后用后缀数组求解.两种方法:1,枚举排名,直接比较rank相同的字符串的位置差是不是len.如果是的话,就记录求解:2,枚举地址,求第i地址与第2*len-i+1的lcp的最大值. PS:需要注意如果多解输出靠前的字符串. 两种写法写在了一起,分别是Del,和Del1函数. 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB T

URAL 1297. Palindrome(后缀数组 求最长回文子串)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1297 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots

后缀数组 POJ 3974 Palindrome &amp;&amp; URAL 1297 Palindrome

题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串中心位置,RMQ询问LCP = min (height[rank[l]+1] to height[rank[r]]),注意的是RMQ传入参数最好是后缀的位置,因为它们在树上的顺序未知,且左边还要+1. #include <cstdio> #include <algorithm> #in

URAL 1297 Palindrome

Palindrome Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 129764-bit integer IO format: %lld      Java class name: (Any) The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states

hdu3608 manacher模板题

给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S 两组case之间由空行隔开(该空行不用处理) 字符串长度len <= 110000Output每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度. Sample Input aaaa abab Sample Out