zoukankan      html  css  js  c++  java
  • POJ 3974 Palindrome | 马拉车模板

    给一个字符串,求最长回文字串有多长


    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #define N 1000005
    using namespace std;
    int n,m,T,p[N*2],ans;
    char s[2*N],t[N];
    void manacher()
    {
        int id=0,pos=0,x=0;
        for (int i=1;i<+n;i++)
        {
    	if (pos>i) x=min(p[id*2-i],pos-i);
    	else x=1;
    	while (s[i-x]==s[i+x]) x++;
    	if (i+x>pos) pos=i+x,id=i;
    	p[i]=x;
        }
    }
    int main()
    {
        while (scanf("%s",t+1) && t[1]!='E')
        {
    	ans=0;
    	m=strlen(t+1);
    	s[n=0]='!';
    	for (int i=1;i<=m;i++)
    	{
    	    s[++n]='#';
    	    s[++n]=t[i];
    	}
    	s[++n]='#';
    	s[n+1]='?';
    	manacher();
    	for (int i=1;i<=n;i++)
    	    ans=max(ans,p[i]);
    	printf("Case %d: %d
    ",++T,ans-1);
        }
        return 0;
    }
    
  • 相关阅读:
    树状数组&线段树
    8月7日小练
    8月6日小练
    LID&LDS 的另外一种算法
    LCS,LIS,LCIS
    8-11-Exercise
    8-10-Exercise
    线段树
    8-7-Exercise
    8-6-Exercise
  • 原文地址:https://www.cnblogs.com/mrsheep/p/7976877.html
Copyright © 2011-2022 走看看