Finding Palindromes
Time Limit: 10000MS   Memory Limit: 262144K
     
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树
用原串的反串在trie树上匹配
假设反串匹配到i,trie树上到j
1、如果j是单词节点,即以j结尾的单词是反串的前缀,那么如果i以后(反串剩余的部分,不包括i)是回文串,以j结尾的单词都可以与匹配的单词构成回文串
2、如果反串匹配完了,即反串是以i结尾的单词的前缀,那么j后面(不包括j)有多少回文串,就可以产生多少合法答案
关键:字符串的前缀回文和后缀回文
用扩展kmp
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 2000008
using namespace std;
bool f[][N];
long long ans;
char T[N],S[N];
int len,tot,root,id;
int st[N],ed[N],cnt;
int nxt[N],expand[N];
int trie[N][],mark[N],sum[N];
void getnxt(char *s,int ll,int rr)
{
int a=ll;
nxt[]=rr-ll+;
while(a+<=rr && s[a]==s[a+]) a++;
nxt[]=a-ll;
a=+ll;
int p,l,j;
for(int k=+ll;k<=rr;k++)
{
p=a-ll+nxt[a-ll]-; l=nxt[k-a];
if(k-ll+l->=p)
{
j=p-k+ll+> ? p-k+ll+ : ;
while(k+j<=rr && s[k+j]==s[j+ll]) j++;
nxt[k-ll]=j;
a=k;
}
else nxt[k-ll]=l;
}
}
void exkmp(char *s,char *t,int ll,int rr,int w)
{
getnxt(t,ll,rr);
int a=ll;
while(a<=rr && s[a]==t[a]) a++;
expand[]=a-ll;
a=ll;
int p,l,j;
for(int k=ll+;k<=rr;k++)
{
p=a-ll+expand[a-ll]-; l=nxt[k-a];
if(k-ll+l->=p)
{
j=p-k+ll+> ? p-k+ll+ : ;
while(k+j<=rr && s[k+j]==t[j+ll]) j++;
expand[k-ll]=j;
a=k;
}
else expand[k-ll]=l;
}
for(int i=ll-ll;i<=rr-ll;i++)
if(i+expand[i]==rr-ll+) f[w][i+ll]=true;
}
void insert(int ll,int rr)
{
root=;
for(int i=ll;i<=rr;i++)
{
id=S[i]-'a';
sum[root]+=f[][i];
if(!trie[root][id]) trie[root][id]=++tot;
root=trie[root][id];
}
mark[root]++;
}
void find(int ll,int rr)
{
root=;
for(int i=ll;i<=rr;i++)
{
id=T[i]-'a';
root=trie[root][id];
if(!root) return;
if(i!=rr&&f[][i+] || i==rr) ans+=mark[root];
}
ans+=sum[root];
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%s",&len,S+cnt);
for(int j=;j<len;j++) T[cnt+j]=S[cnt+len-j-];
st[i]=cnt;
ed[i]=cnt+len-;
exkmp(S,T,st[i],ed[i],);
exkmp(T,S,st[i],ed[i],);
cnt+=len;
insert(st[i],ed[i]);
}
for(int i=;i<=n;i++)
find(st[i],ed[i]);
printf("%lld\n",ans);
}

poj 3376 Finding Palindromes的更多相关文章

  1. POJ - 3376 Finding Palindromes(拓展kmp+trie)

    传送门:POJ - 3376 题意:给你n个字符串,两两结合,问有多少个是回文的: 题解:这个题真的恶心,我直接经历了5种错误类型 : ) ... 因为卡内存,所以又把字典树改成了指针版本的. 字符串 ...

  2. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

  3. POJ 3376 Finding Palindromes(manacher求前后缀回文串+trie)

    题目链接:http://poj.org/problem?id=3376 题目大意:给你n个字符串,这n个字符串可以两两组合形成n*n个字符串,求这些字符串中有几个是回文串. 解题思路:思路参考了这里: ...

  4. POJ 3376 Finding Palindromes EX-KMP+字典树

    题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...

  5. POJ 3376 Finding Palindromes (tire树+扩展kmp)

    很不错的一个题(注意string会超时) 题意:给你n串字符串,问你两两匹配形成n*n串字符串中有多少个回文串 题解:我们首先需要想到多串字符串存储需要trie树(关键),然后我们正序插入倒序匹配就可 ...

  6. POJ - 3376 Finding Palindromes manacher+字典树

    题意 给n个字符串,两两拼接,问拼接后的\(n\times n\)个字符串中有多少个回文串. 分析 将所有正串插入字典树中,马拉车跑出所有串哪些前缀和后缀为回文串,记录位置,用反串去字典树中查询,两字 ...

  7. poj3376 Finding Palindromes【exKMP】【Trie】

    Finding Palindromes Time Limit: 10000MS   Memory Limit: 262144K Total Submissions:4710   Accepted: 8 ...

  8. POJ3376 Finding Palindromes —— 扩展KMP + Trie树

    题目链接:https://vjudge.net/problem/POJ-3376 Finding Palindromes Time Limit: 10000MS   Memory Limit: 262 ...

  9. POJ 2049— Finding Nemo(三维BFS)10/200

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...

随机推荐

  1. [C++] String Basic

    Namespace Declarations A using declaration let us use a name from a namespace without qualify the na ...

  2. JS中Text节点总结

    文本节点有Text类型表示,包含的是可以找字面解释的纯文本内容.纯文本中可以包含转移后的HTML字符,但不能包含HTML代码.Text节点具有以下属性: 1.nodeType的值为3. 2.nodeN ...

  3. 拓扑排序(Toposort)

    摘自:https://blog.csdn.net/qq_35644234/article/details/60578189 <图论算法> 1.拓扑排序的介绍 对一个有向无环图(Direct ...

  4. Ubuntu16.04修改IP

    首先用root用户登陆,然后输入你root的密码.如下图:   然后编辑interfaces文件,该文件位于/etc/network/下,执行如下命令: vim /etc/network/interf ...

  5. lintcode-6-合并排序数组

    合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果其中一个 ...

  6. .net Mvc4 View—布局页与分部页

    ▲      一.Layout属性     1.1.@RenderPage():渲染制定的页面到占位符.     注意:@RenderPage()可以使用多次,这点与@RenderBody()不同   ...

  7. BAT批处理(四)

    网络命令 net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" / ...

  8. CSS设计指南之CSS三种机制:继承、层叠和特指

    一.继承 CSS中的祖先元素也会向后代传递一样东西:CSS属性的值.body元素是所有元素的祖先,所有标签都是它的后代,比如为body元素设置一种字体,那么文档中的所有元素,无论它在层次结构中多么靠下 ...

  9. 苹果IOS、安卓推送功能开发

    IOS推送开发:以下是基于开源javapns推送开发1.DerInputStream.getLength(): lengthTag=111, too big.先排除是否由于打包时证书 .p12 文件被 ...

  10. 【swaggerui】swaggerui在asp.net web api core 中的应用

    Swaggerui 可以为我们的webapi提供美观的在线文档,如下图: 实现步骤: NuGet Packages  Install-Package Swashbuckle.AspNetCore 在s ...