poj3376 KMP+字典树求回文串数量(n*n)

Posted Billyshuai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3376 KMP+字典树求回文串数量(n*n)相关的知识,希望对你有一定的参考价值。

Finding Palindromes
Time Limit: 10000MS   Memory Limit: 262144K
Total Submissions: 4043   Accepted: 746
Case Time Limit: 2000MS

Description

A word is called a palindrome if we read from right to left is as same as we read from left to right. For example, "dad", "eye" and "racecar" are all palindromes, but "odd", "see" and "orange" are not palindromes.

Given n strings, you can generate n × n pairs of them and concatenate the pairs into single words. The task is to count how many of the so generated words are palindromes.

Input

The first line of input file contains the number of strings n. The following n lines describe each string:

The i+1-th line contains the length of the i-th string li, then a single space and a string of li small letters of English alphabet.

You can assume that the total length of all strings will not exceed 2,000,000. Two strings in different line may be the same.

Output

Print out only one integer, the number of palindromes.

Sample Input

3
1 a
2 ab
2 ba

Sample Output

5

Hint

The 5 palindromes are:
aa aba aba abba baab
 
 
http://blog.csdn.net/acblacktea/article/details/51884850  //转载这篇博客
 
#include<cstdio>
#include<cstring>
#include<vector>
typedef long long LL;
using namespace std;
const int N=2000005;
struct node{
    int sum,son[26],cnt;
}no[N];
struct str{
    int start,en;
    str(int s,int e):start(s),en(e){};
    str(){};
};
vector<str>ve;
int fla[N][2],nexta[N],tot=0,n,pre,l;
char s[N],t[N];
void getNext(char *a,int la){
    int i=0,j=-1;
    nexta[0]=-1;
    while(i<la){
        if(j==-1||a[i]==a[j]) nexta[++i]=++j;
        else j=nexta[j];
    }
}
void kmp(int flag,char *a,char *b,int la,int start){
    int i=0,j=0;
    while(i<la){
        if(j==-1||a[j]==b[i]) ++i,++j;
        else j=nexta[j];
    }
    int pre=j;
    if(!flag) {
        while(pre){
            fla[start+pre-1][0]=1;
            pre=nexta[pre];
        }
    }
    else {
        while(pre){
            fla[start+l-pre][1]=1;
            pre=nexta[pre];
        }
    }
}
void insertTire(int pre,char *a,int start,int la)//储存前缀
{
     for(int i=0;i<l;i++)
     {
         if(!no[pre].son[a[i]-a])
         {
             tot++;
             no[pre].son[a[i]-a] = tot;
             pre = tot;
         }
         else pre = no[pre].son[a[i]-a];
         if(i+1<la)no[pre].cnt+=fla[start+i+1][1];
     }
     no[pre].sum++;
}
LL findTrie(int start,int en,int pre){
    int sym=true;
    LL ans=0;
    int l=en-start;
    for(int i=start;i<en;++i) {
        if(no[pre].son[t[i]-a]) {
            pre=no[pre].son[t[i]-a];
            if(fla[start+l-(i-start+1)-1][0]||i==en-1) ans+=no[pre].sum;
        }
        else {
            sym=0;break;
        }
    }
    if(sym) ans+=no[pre].cnt;
    return ans;
}
int main(){
    pre=0;
    LL ans=0;
    for(scanf("%d",&n);n--;){
        scanf("%d %s",&l,s+pre);
        ve.push_back(str(pre,pre+l));
        for(int i=pre;i<pre+l;++i) t[i]=s[pre+l-(i-pre+1)];
        getNext(s+pre,l);
        kmp(0,s+pre,t+pre,l,pre);
        getNext(t+pre,l);
        kmp(1,t+pre,s+pre,l,pre);
        insertTire(0,s+pre,pre,l);
        pre+=l;
    }
    for(int i=0;i<(int)ve.size();++i) ans+=findTrie(ve[i].start,ve[i].en,0);
    printf("%I64d\n",ans);
}

 

以上是关于poj3376 KMP+字典树求回文串数量(n*n)的主要内容,如果未能解决你的问题,请参考以下文章

POJ - 1159 Palindrome(回文变形)

kmp-最小子串回文次数

hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串

POJ-2406Power Strings-KMP+定理

CF17E Palisection

POJ - 2406 ~SPOJ - REPEATS~POJ - 3693 后缀数组求解重复字串问题