POJ 3376 Finding Palindromes

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


【题目大意】
给定n个字符串,共有n*n种组合的方式,那么产生了一个问题,总共有多少个是回文串,字符串的总长度不超过200w


【题目分析】
    好坑啊,总长不超过200w,是200w个长度为1还是一个长度为200w,所以只能用一个数组存所有的字符串了。
    然后再来考虑这道题目,如何减少计算的数量,假设有两个串cba和abcded,我们把第二个串插入到trie树里,然后就可以拿第一个字符串反向的去比较了。我们可以把第一个串反过来,然后比较,发现可以匹配前三个(trie树中进行转移十分方便)就可以剩下ded三个字符,而他们显然是一个回文串,所以答案+1.所以我们只需要标记出来trie树种所有节点的儿子中是回文串的数量,然后去反向匹配到终点,最后加入答案即可,如果到了一定位数失陪了,而且发现当前结点是一个字符串结束的位置,而且剩余串是回文串,这时候答案也需要增加,举个例子,我们先插入abc 然后遇到了一个串dedcba,我们反过来匹配的时候发现到了c的时候无法匹配,剩下的是一个回文串,而且恰好当前位置有一个字符串结束,答案+1。然后就可以了。至于一些细枝末节的东西就自己慢慢改好了。别人的代码好长,许多人都是用KMP的,用manacher明显会好些很多啊。
   


【代码】

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n,trie[2000010][27],tot=1,root=0,st[2000010],l[2000010],cnt[2000010],r[4000010],en=0;
char s[2000010];
int end[2000010];
char a[4000010];
int li[4000010],nxt[4000010];
long long ans=0;
inline void insert()
{
    a[0]='$';a[1]='#';
    tot--;
    for (int i=1;i<=tot;++i)
    {
        a[i*2]=s[i];
        a[i*2|1]='#';
    }
    a[tot*2+2]='@';
    int id=0,mx=0;
    for (int i=1;i<=tot*2+1;++i)
    {
        if (i<mx) r[i]=min(r[2*id-i],mx-i); else r[i]=0;
        while (a[i-r[i]]==a[i+r[i]]) r[i]++;
        if (i+r[i]>mx) mx=i+r[i],id=i;
    }
    for (int i=1;i<=n;++i)
    {
        int now=root;
        for (int j=st[i];j<=st[i]+l[i]-1;++j)
        {
            if (!trie[now][s[j]-'a']) trie[now][s[j]-'a']=++en;
            now=trie[now][s[j]-'a'];
            int mid=((j+1)*2-1+(st[i]+l[i]-1)*2+1)/2;
            if (r[mid]>mid-(j+1)*2+1)
            {
                cnt[now]++;
            }
        }
        cnt[now]--;
        end[now]++;
    }
}
inline void find()
{
    for (int i=1;i<=n;++i)
    {

        int now=root;
        for (int j=st[i]+l[i]-1;j>=st[i];--j)
        {
            if (!trie[now][s[j]-'a']) {now=-1;break;}
            now=trie[now][s[j]-'a'];
            if (end[now])
            {
                int mid=(st[i]*2-1+(j-1)*2+1)/2;
                if (r[mid]>mid-st[i]*2+1)
                    ans+=end[now];
            }
        }
        if (now!=-1) ans+=(long long)cnt[now];
    }
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        en=0,tot=1;ans=0;
        memset(trie,0,sizeof trie);
        memset(cnt,0,sizeof cnt);
        memset(end,0,sizeof end);
        for (int i=1;i<=n;++i)
        {
            scanf("%d",&l[i]);
            scanf("%s",s+tot);
            st[i]=tot;tot+=l[i];
        }
        insert();
        find();
        cout<<ans<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值