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:
a a a ba ab a ab ba ba ab
 
 
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);
}

 

转载于:https://www.cnblogs.com/mfys/p/7401812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值