HDOJ 3068 最长回文

题意:给出一个字符串,找出其中最长回文字符串的长度

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068

思路:Manacher回文串模板

注意点:无


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
130378102015-03-05 22:51:00Accepted3068468MS2276K2392 BG++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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值