poj 3974 Palindrome

#include <iostream>
#include <string>
const int MAX = 1000005;
char str[MAX], Manacher_str[2*MAX];
int len, tc = 0, p[2*MAX];
using namespace std;

void change()
{
     int i; 
     Manacher_str[0] = '@';
     Manacher_str[1] = '#';
     for (i = 0; i < len; i++){
         Manacher_str[2*i + 2] = str[i];
         Manacher_str[2*i + 3] = '#';
     }
     Manacher_str[2*len + 2] = '\0';
}


void Manacher()
{
     int i, maxid = 0, id, ans = 1;
     len = len * 2 + 2;
     for (i = 0; i < len; i++){
         if (maxid > i)
             p[i] = min(p[2*id - i], maxid - i);
         else
             p[i] = 1;
         while (Manacher_str[i-p[i]] == Manacher_str[i+p[i]])
                p[i]++;
         if (p[i] + i > maxid){
             maxid = p[i] + i;
             id = i;
         }
         if (p[i] > ans)
             ans = p[i];
     }
     
     cout << "Case " << tc << ": " << ans - 1 << endl;
}


int main()
{
    while (scanf("%s", &str)){
          if (strcmp(str, "END") == 0)  break;
          tc++;
          len = strlen(str);
          change();
          Manacher();
    }
    
    system("pause");
}


看Manacher算法的请看:http://blog.csdn.net/yzl_rex/article/details/7908259

你可能感兴趣的:(算法,System,include)