题意:给出一个字符串,找出其中最长回文字符串的长度
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068
思路:Manacher回文串模板
注意点:无
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
13037810 | 2015-03-05 22:51:00 | Accepted | 3068 | 468MS | 2276K | 2392 B | G++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
//#include <unordered_map>
//#include <unordered_set>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define RS(s) scanf ( "%s", s )
#define PIL(a) printf ( "%d\n", a )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define PSL(s) printf ( "%s\n", s )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
int x, y, cnt;
node(){}
node( int _x, int _y ) : x(_x), y(_y) {}
node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
const int maxn = 110010;
char ma[maxn<<1];
int mp[maxn<<1];
void manacher ( char s[], int len )
{
int l = 0;
ma[l++] = '$';
ma[l++] = '#';
REP ( i, 0, len - 1 ){
ma[l++] = s[i];
ma[l++] = '#';
}
ma[l] = 0;
int mx = 0;
int id = 0;
REP ( i, 1, l - 1 ){
mp[i] = mx > i ? min ( mp[2*id-i], mx-i ) : 1;
while ( ma[i+mp[i]] == ma[i-mp[i]] )mp[i] ++;
if ( i + mp[i] > mx ){
mx = i + mp[i];
id = i;
}
}
}
char buf[maxn];
int main()
{
while ( RS( buf ) != EOF ){
int len = strlen( buf );
manacher ( buf, len );
int ans = 0;
REP ( i, 0, 2 * len + 1 ){
ans = max ( ans, mp[i] - 1 );
}
PIL ( ans );
}
return 0;
}