首页 > 代码库 > POJ 3376 Finding Palindromes

POJ 3376 Finding Palindromes

http://poj.org/problem?id=3376

Finding Palindromes
Time Limit: 10000MS   Memory Limit: 262144K
Total Submissions: 3973   Accepted: 726
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

思路,trie加manacher算法,将所有的字符串反向插入字典树中,记录结尾和字符串中回文串的个数,然后每个字符串通过manacher找到回文串的前后缀;

这样插入到字符串中时,前缀就变成了后缀,在查询时,相当于拿前缀查找反转后的前缀,相当于后缀,剩下的根据字符串的特性,如果中查找到就说明可以完全匹配;

再提交时,用指针写,一直时间超限,改为结构体数组后就可以过了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
int p[4000009],a[2000009];
char s[2000009],ss[4000009];
bool ft[2000006],lt[2000009];
int pos;
struct Trie
{
    int next[26];
    int leaf;
    int cnt;
    void init()
    {
        leaf=cnt=0;
        memset(next,false,sizeof(next));
    }
}trie[2000006];
void buildtrie(int n)
{
    int x=0;
    for(int i=a[n+1]-1;i>=a[n];i--)
    {
        int k=s[i]-a;
        trie[x].cnt+=ft[i];//倒着插前缀变后缀
        if(trie[x].next[k]==0)
        {
            trie[x].next[k]=++pos;
            trie[pos].init();
        }
        x=trie[x].next[k];
    }
    trie[x].leaf+=1;
}
void manacher(int n)
{
    int id=0,l=1;
    ss[0]=@;
    for(int i=a[n];i<a[n+1];i++)
    {
        ss[l++]=#;
        ss[l++]=s[i];
        ft[i]=false;
        lt[i]=false;
    }
    ss[l]=#;
    ss[l+1]=0;
    p[0]=0;
    for(int i=2;i<l;i++)
    {
        p[i]=1;
        if(id+p[id]>i)p[i]=min(p[id*2-i],p[id]+id-i);
        while(ss[i+p[i]]==ss[i-p[i]]) p[i]++;
        if(p[id]+id<i+p[i]) id=i;
        if(p[i]==i) ft[a[n]+p[i]-2]=true;
        if(p[i]+i-1==l) lt[a[n+1]-p[i]+1]=true;
    }
}
int query(int n)
{
    int i,x=0,ans=0;
    for(i=a[n];i<a[n+1];i++)
    {
        int k=s[i]-a;
        if(trie[x].next[k]==0)
            break;
        x=trie[x].next[k];
        if(lt[i+1] || i==a[n+1]-1)//匹配后缀
            ans+=trie[x].leaf;
    }
    if(i==a[n+1])
        ans+=trie[x].cnt;//完全匹配
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    pos=0;
    trie[0].init();
    int l;
    a[0]=0;
    for(int i=1;i<=t;i++)
    {
        scanf("%d%s",&l,s+a[i]);
        a[i+1]=a[i]+l;
        manacher(i);
        buildtrie(i);
    }
    ll ans=0;
    for(int i=1;i<=t;i++)
        ans+=query(i);
    printf("%lld\n",ans);
    return 0;
}

再来一个时间超限的指针代码

技术分享
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
int p[4000009],a[2000009];
char s[2000009],ss[4000009];
bool ft[2000006],lt[2000009];
struct trie
{
    trie *next[26];
    int leaf;
    int cnt;
    trie()
    {
        leaf=cnt=0;
        for(int i=0;i<26;i++)
            next[i]=NULL;
    }
};
trie *root;
void buildtrie(int n)
{
    trie *p=root;
    trie *tmp;
    for(int i=a[n+1]-1;i>=a[n];i--)
    {
        int k=s[i]-a;
        p->cnt+=ft[i];
        if(p->next[k]==NULL)
        {
            tmp=new trie();
            p->next[k]=tmp;
        }
        p=p->next[k];
    }
    p->leaf+=1;
}
void manacher(int n)
{
    int id=0,l=1;
    ss[0]=@;
    for(int i=a[n];i<a[n+1];i++)
    {
        ss[l++]=#;
        ss[l++]=s[i];
        ft[i]=false;
        lt[i]=false;
    }
    ss[l]=#;
    ss[l+1]=0;
    p[0]=0;
    for(int i=2;i<l;i++)
    {
        p[i]=(id+p[id]>i)?min(p[id*2-i],p[id]+id-i):1;
        while(ss[i+p[i]]==ss[i-p[i]]) p[i]++;
        if(p[id]+id<i+p[i]) id=i;
        if(p[i]==i) ft[a[n]+p[i]-2]=true;
        if(p[i]+i-1==l) lt[a[n+1]-p[i]+1]=true;
    }
}
int query(int n)
{
    trie *p=root;
    int i,ans=0;
    for(i=a[n];i<a[n+1];i++)
    {
        int k=s[i]-a;
        if(p->next[k]==NULL)
            break;
        p=p->next[k];
        if(lt[i+1] || i==a[n+1]-1)
            ans+=p->leaf;
    }
    if(i==a[n+1])
        ans+=p->cnt;
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    int l;
    root=new trie();
    a[0]=0;
    for(int i=1;i<=t;i++)
    {
        scanf("%d%s",&l,s+a[i]);
        a[i+1]=a[i]+l;
        manacher(i);
        buildtrie(i);
    }
    ll ans=0;
    for(int i=1;i<=t;i++)
        ans+=query(i);
    printf("%lld\n",ans);
    return 0;
}
Time Limit Exceeded code

 

POJ 3376 Finding Palindromes