博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3376 Finding Palindromes
阅读量:7080 次
发布时间:2019-06-28

本文共 3039 字,大约阅读时间需要 10 分钟。

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

31 a2 ab2 ba

Sample Output

5

Hint

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

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/7050413.html

你可能感兴趣的文章
Mac OSX操作系统安装和配置Zend Server 6教程(4)
查看>>
安装homeassistant+python3.6
查看>>
ubuntu下chrome无法同步问题解决
查看>>
搭建Nginx+Java环境(转)
查看>>
pc端车牌识别在智能机器人上的应用
查看>>
余弦相似度计算
查看>>
Koa (koajs) 基于 Node.js 平台的下一代 web 开发框架
查看>>
大型网站技术架构(六)网站的伸缩性架构
查看>>
多表查询
查看>>
理解作用域(引擎,编译器,作用域)
查看>>
获取网页数据的例子
查看>>
struts2的配置文件
查看>>
JSP第5次测试---测试分析
查看>>
tomcat容器
查看>>
同时可以修改时间和日期的datetime_select and 有关时间的转换
查看>>
IOS Orientation, 想怎么转就怎么转~~~
查看>>
Finding Lines
查看>>
服务提供者及门面
查看>>
POJ-1611-The Suspects(并查集)
查看>>
用VC生成 IDispatch 包装类
查看>>