PAT (Advanced Level) Practise 1040 Longest Symmetric String (25)
2017-07-19 11:37
330 查看
1040. Longest Symmetric String (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
题意:给你一个字符串,输出它的回文串最大长度
解题思路:Manacher算法
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> using namespace std; #define ll long long char s1[110009],s2[220009]; int p[220009]; int main() { while(gets(s1)) { int i=0,len=1; s2[0]='$'; while(s1[i]!='\0') { s2[len++]='#'; s2[len++]=s1[i]; i++; } s2[len++]='#'; s2[len]='\0'; memset(p,0,sizeof p); int id=0,ma=0; for(int i=0;i<len;i++) { p[i]=ma>i?min(p[id*2-i],ma-i):1; while(s2[i+p[i]]==s2[i-p[i]]) p[i]++; if(i+p[i]>ma) { ma=i+p[i]; id=i; } } int ans=0; for(int i=0;i<len;i++) ans=max(ans,p[i]-1); printf("%d\n",ans); } return 0; }
相关文章推荐
- PAT (Advanced Level) Practise 1040 Longest Symmetric String (25)
- Pat(Advanced Level)Practice--1040(Longest Symmetric String)
- PAT (Advanced Level) Practise 1105 Spiral Matrix (25)
- PAT (Advanced Level) Practise - 1002. A+B for Polynomials (25)
- 1051. Pop Sequence (25)【栈】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1029. Median (25)
- 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise
- 1017. Queueing at Bank (25) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1079 Total Sales of Supply Chain (25)
- PAT (Advanced Level) Practise 1024 Palindromic Number (25)
- pat 1040 Longest Symmetric String
- PAT (Advanced Level) Practise 1002 A+B for Polynomials (25)
- PAT (Advanced Level) Practise 1117 Eddington Number(25)
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
- 1017. Queueing at Bank (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1017. Queueing at Bank (25)
- 1002. A+B for Polynomials (25)——PAT (Advanced Level) Practise
- 1012. The Best Rank (25)——PAT (Advanced Level) Practise
- 1029. Median (25)【排序】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1062 Talent and Virtue (25)