uva11888 - Abnormal 89's Manacher回文子串

 Abnormal 89's 

A palindrome is a word that can be read the same way in either direction. More formally if a string isd (d > 0) characters length and the i-th character is ai, the string is palindrome if and only if ai equalsa(d-i+1) for 1id. For example "abcba" is palindrome while "aaab" is not.

It is known that everyone who gets to know palindromes, begin an emotional relationship with thesebeautiful strings. The harmony between the letters makes them artistic. But the 89's (those whoentered AUT at 1389) claim they love another kind of strings. It is called alindrome. Actually analindrome is the result of concatenation of two palindromes. For example "abacc"="aba"+"cc" isalindrome.

Now you should write a program to distinguish alindromes, palindromes and other kind of strings.

Input 

The first line contains T ( T50), the number of tests. Each test that comes in a separate line containsa string to be checked. Input strings contain only lower case letters ( ' a' to ' z' ) and their length will be at most 200000.

Output 

For each test output a single word in a single line. If the input string can be made by concatenatingtwo palindromes, output " alindrome". Otherwise if it’s a palindrome output " palindrome". Inany other case print " simple". (Quotes for clarity)

Sample Input 

4
aaa
aabaa
aabaaa
abc

Sample Output 

alindrome
palindrome
alindrome
simple

  如果是两个回文串拼起来的输出alindrome,否则如果是一个回文串输出palindrome,否则输出simple。

  Manacher算法,我的思路是在算p[i]的时候如果当前i扩展到了最后,就判断除这个回文子串之外前面的那一部分是不是回文串,也就是判断前面那一部分的中心点j是否有p[j]==j,并且要保证这两个中心点不能是最前或者最后的那个分隔符,因为必须要保证这个串被分成了两部分。加了分隔符后位置容易算错,要想清楚。。

  如果不满足两个拼起来,再判断是不是回文串,如果p[len+1]==len+1就是,其中len是加了分隔符后的,等于原来的len*2+2。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define INF 0x3f3f3f3f
#define MAXM 55
#define LOGMAXN 10
#define MAXNODE 100010*4
#define MOD 1000000007
typedef long long LL;
using namespace std;

const int MAXN=200010*2;

int N,p[MAXN];
char str[MAXN];

bool manacher(char* str){
    int len=strlen(str);
    for(int i=len;i>=0;i--){
        str[2*i+2]=str[i];
        str[2*i+1]='#';
    }
    str[0]='*';
    len=len*2+2;
    int maxp=0,id;
    for(int i=1;i<len-1;i++){
        if(i<maxp) p[i]=min(maxp-i,p[id*2-i]);
        else p[i]=1;
        while(str[i-p[i]]==str[i+p[i]]) p[i]++;
        if(i+p[i]>maxp){
            maxp=i+p[i];
            id=i;
        }
        if(i+p[i]==len&&(i-p[i]+2)/2>1&&p[(i-p[i]+2)/2]==(i-p[i]+2)/2) return true;
    }
    return false;
}

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N)!=EOF){
        for(int i=0;i<N;i++){
            scanf("%s",str);
            int len=strlen(str);
            if(manacher(str)) printf("alindrome\n");
            else if(p[len+1]==len+1) printf("palindrome\n");
            else printf("simple\n");
        }
    }
    return 0;
}



你可能感兴趣的:(uva11888 - Abnormal 89's Manacher回文子串)