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=;
struct node{
int sum,son[],cnt;
}no[N];
struct str{
int start,en;
str(int s,int e):start(s),en(e){};
str(){};
};
vector<str>ve;
int fla[N][],nexta[N],tot=,n,pre,l;
char s[N],t[N];
void getNext(char *a,int la){
int i=,j=-;
nexta[]=-;
while(i<la){
if(j==-||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=,j=;
while(i<la){
if(j==-||a[j]==b[i]) ++i,++j;
else j=nexta[j];
}
int pre=j;
if(!flag) {
while(pre){
fla[start+pre-][]=;
pre=nexta[pre];
}
}
else {
while(pre){
fla[start+l-pre][]=;
pre=nexta[pre];
}
}
}
void insertTire(int pre,char *a,int start,int la)//储存前缀
{
for(int i=;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+<la)no[pre].cnt+=fla[start+i+][];
}
no[pre].sum++;
}
LL findTrie(int start,int en,int pre){
int sym=true;
LL ans=;
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+)-][]||i==en-) ans+=no[pre].sum;
}
else {
sym=;break;
}
}
if(sym) ans+=no[pre].cnt;
return ans;
}
int main(){
pre=;
LL ans=;
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+)];
getNext(s+pre,l);
kmp(,s+pre,t+pre,l,pre);
getNext(t+pre,l);
kmp(,t+pre,s+pre,l,pre);
insertTire(,s+pre,pre,l);
pre+=l;
}
for(int i=;i<(int)ve.size();++i) ans+=findTrie(ve[i].start,ve[i].en,);
printf("%I64d\n",ans);
}

poj3376 KMP+字典树求回文串数量(n*n)的更多相关文章

  1. Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串

    E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. 拓展KMP求回文串

    题目:hdu3613: 题意:有26字母对应的价值,然后给出以个串,把它分成两段字串,如果字串是回文串,串的价值就是每个字符和,不是就为0.求最大价值. 博客 分析:拓展KMP的应用求回文字串. #i ...

  3. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  4. Palindrome - URAL - 1297(求回文串)

    题目大意:RT   分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的.   线段数+后缀数组代码如下: ...

  5. manacher算法,求回文串

    用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...

  6. 马拉车,O(n)求回文串

    马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...

  7. HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )

    题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...

  8. 3676: [Apio2014]回文串 求回文串长度与出现次数的最大值

    「BZOJ3676」[Apio2014] 回文串   Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所 ...

  9. hdu 3294 manacher 求回文串

    感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...

随机推荐

  1. Linux系统管理第三次作业 账号管理 权限及归属管理

    1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) [root@localhost ~]# mkdir /guanli [root@localhos ...

  2. 09-5.部署 EFK 插件

    09-5.部署 EFK 插件 EFK 对应的目录:kubernetes/cluster/addons/fluentd-elasticsearch $ cd /opt/k8s/kubernetes/cl ...

  3. 狄慧201771010104《面向对象程序设计(java)》第十六周学习总结

    实验十六  线程技术 实验时间 2017-12-8 一.知识点总结: 1.程序与进程的概念 ‐程序是一段静态的代码,它是应用程序执行的蓝本. ‐进程是程序的一次动态执行,它对应了从代码加载.执行至执行 ...

  4. windows 7或以上系统的实用小工具,你知道么?

    今晚给大家介绍个实用的好工具,可以做简单的问题记录,再也不用截图加注释这么辛苦了····· 经测试,这东东在win7,2008 及2008R2里适用,也就是说,在win7以上的系统中才有.好了,下面直 ...

  5. JAVA编程思想 Ch3.6题

    练习6:在练习5的基础上,创建一个新的Dog索引,并对其赋值为Spot对象.测试用==和equals()方法来比较引用结果. public class quan { String name; Stri ...

  6. C++编程入门题目--No.5

    题目: 输入三个整数x,y,z,请把这三个数由小到大输出. 程序分析: 我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换, 然后再用x与z进行比较,如果x> ...

  7. c/c++获取文件夹下所有文件名

    如何获取某一文件夹下所有文件名,是一个很有意思的问题.网上代码很多,找了个简单的,特此收录. #include <iostream> #include <io.h> #incl ...

  8. 用Navicat建MySQL数据库表,动态改变创建时间和更新时间戳

    1.create_time 记录创建的时间,设默认值为:CURRENT_TIMESATMP 注意:不勾选那个[根据当前时间戳更新] 2.operator_time 更新记录的时间,勾选那个[根据当前时 ...

  9. [蓝桥杯2018初赛]小朋友崇拜圈(dfs找环)

    传送门 思路: 题意大意:n条有向边,找出最大环. 我们发现,如果一个小朋友没有被任何人崇拜,那么他一定不位于环中.为此我们可以设置一个indug数组预处理.如果2被崇拜了那么indug[2]就加加, ...

  10. uniapp滚动监听元素

    鸽了这么久,一晃2个月过去了.自考+上班没时间记录. 前不久看到移动官网上的时间轴效果,看起来不错,我也来试着做一下. 需要元素滚动到视野内加载动画. 插件地址 https://ext.dcloud. ...