POJ3376 Finding Palindromes —— 扩展KMP + Trie树
题目链接:https://vjudge.net/problem/POJ-3376
Time Limit: 10000MS | Memory Limit: 262144K | |
Total Submissions: 4244 | Accepted: 796 | |
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
aa aba aba abba baab
Source
题解:
可知:如果两字符串拼接起来能够形成回文串,那么短串的逆串是长串的前缀。
根据上述结论,我们可以:
1.利用扩展KMP算法求出每个字符串的后缀是否为回文串,以及每个字符串的逆串的后缀是否为回文串。
2.将每个字符串插入到Trie树中,并且Trie树的结点需要维护两个信息:当前结点的字符串数,以及往下有多少个回文串后缀。
3.用每个字符串的逆串去Trie树中查找。有两种情况:当前串作为长串,以及当前串作为短串。详情请看代码。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MAXN = 2e6+; char str[MAXN], res[MAXN];
int beg[MAXN], len[MAXN], ispal[][MAXN];
int Next[MAXN], exd[MAXN]; struct Node
{
int strnum;
int palnum;
int nxt[];
};
Node node[MAXN];
int tot, root; int newnode()
{
node[tot].strnum = ;
node[tot].palnum = ;
memset(node[tot].nxt, -, sizeof(node[tot].nxt));
tot++;
return tot-;
} void pre_EXKMP(char x[], int m)
{
Next[] = m;
int j = ;
while(+j<m && x[+j]==x[+j]) j++;
Next[] = j;
int k = ;
for(int i = ; i<m; i++)
{
int p = Next[k]+k-;
int L = Next[i-k];
if(i+L<=p) Next[i] = L;
else
{
j = max(, p-i+);
while(i+j<m && x[i+j]==x[+j]) j++;
Next[i] = j;
k = i;
}
}
} void EXKMP(char x[], int m, char y[], int n, int _L, int type)
{
pre_EXKMP(x, m);
int j = ;
while(j<n && j<m && x[j]==y[j]) j++;
exd[] = j;
int k = ;
for(int i = ; i<n; i++)
{
int p = exd[k]+k-;
int L = Next[i-k];
if(i+L<=p) exd[i] = L;
else
{
j = max(,p-i+);
while(i+j<n && j<m && y[i+j]==x[+j]) j++;
exd[i] = j;
k = i;
}
} //当type为0时,求的是字符串的后缀是否为回文串
//当type为1时,求的是字符串的逆串是否为回文串。实际就是求字符串的前缀是否为回文串。
for(int i = ; i<n; i++) //判断后缀是否为回文串
ispal[type][_L+i] = (i+exd[i]==n);
} void insert(char x[], int n, int L)
{
int tmp = root;
for(int i = ; i<n; i++)
{
int ch = x[i]-'a';
node[tmp].palnum += ispal[][L+i]; //如果后缀是回文串,就把统计数放在父节点。
if(node[tmp].nxt[ch]==-) node[tmp].nxt[ch] = newnode();
tmp = node[tmp].nxt[ch];
}
node[tmp].strnum++; //字符串数+1
} int search(char x[], int n, int L)
{
int ret = ;
int tmp = root;
for(int i = ; i<n; i++)
{
int ch = x[i]-'a';
tmp = node[tmp].nxt[ch];
if(tmp==-) break;
if((i<n-&&ispal[][L+i+]) || i==n-) //x作为长串,如果剩下的部分(后缀)是回文串
ret += node[tmp].strnum;
}
if(tmp!=-) ret += node[tmp].palnum; //x作为短串,加上后缀是回文串的长串
return ret;
} int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
tot = ;
root = newnode(); int L = ;
memset(ispal, , sizeof(ispal));
for(int i = ; i<n; i++)
{
//因为不确定每个字符串的最长长度,所以只好用地址的形式存储字符串
//用string的话,速度会变慢
scanf("%d%s", &len[i], str+L);
beg[i] = L;
L += len[i];
memcpy(res+beg[i], str+beg[i], len[i]);
reverse(res+beg[i], res+beg[i]+len[i]); EXKMP(res+beg[i], len[i], str+beg[i], len[i], beg[i], ); //求字符串后缀是否为回文串
EXKMP(str+beg[i], len[i], res+beg[i], len[i], beg[i], ); //求字符串的逆串的后缀(即字符串的前缀)是否为回文串。
insert(str+beg[i], len[i], beg[i]); //插入Trie树中
} LL ans = ;
for(int i = ; i<n; i++)
ans += search(res+beg[i], len[i], beg[i]); //用字符串的逆串去查找 printf("%lld\n", ans);
}
}
POJ3376 Finding Palindromes —— 扩展KMP + Trie树的更多相关文章
- POJ 3376 Finding Palindromes(扩展kmp+trie)
题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...
- poj3376 Finding Palindromes【exKMP】【Trie】
Finding Palindromes Time Limit: 10000MS Memory Limit: 262144K Total Submissions:4710 Accepted: 8 ...
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
[KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...
- KMP 、扩展KMP、Manacher算法 总结
一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...
- POJ 3376 Finding Palindromes (tire树+扩展kmp)
很不错的一个题(注意string会超时) 题意:给你n串字符串,问你两两匹配形成n*n串字符串中有多少个回文串 题解:我们首先需要想到多串字符串存储需要trie树(关键),然后我们正序插入倒序匹配就可 ...
- 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...
- 【9.15校内测试】【寻找扩展可行域+特判】【Trie树 异或最小生成树】【模拟:)】
之前都没做出来的同名题简直留下心理阴影啊...其实这道题还是挺好想的QAQ 可以发现,鸟可以走到的点是如下图这样扩展的: 由$(0,0)$向两边扩展,黑色是可以扩展到的点,红色是不能扩展的点,可以推出 ...
- poj3376 KMP+字典树求回文串数量(n*n)
Finding Palindromes Time Limit: 10000MS Memory Limit: 262144K Total Submissions: 4043 Accepted: ...
- poj 3376 Finding Palindromes
Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS Memory Limit: 262144K ...
随机推荐
- BS4(BeautifulSoup4)的使用--find_all()篇
可以直接参考 BS4文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all 注意的是: 1.有些 ...
- 小Z的袜子(bzoj 2038)
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- Codevs 2956 排队问题
2956 排队问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3人组成一组,求有多少种不 ...
- PatentTips - GPU Support for Blending
Graphics processing units (GPUs) are specialized hardware units used to render 2-dimensional (2-D) a ...
- call 和 apply 方法区别
在js中call和apply它们的作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别,下面我来给大家介绍一下call和apply用法. 在web前端开发过程中,我们经常需要改变th ...
- js react 全选和反选
onCheckAll = (data) => { var CheckBox = document.getElementsByName(data); for(let i=0;i<CheckB ...
- python3.x对python2.x变动
原文地址:http://rookiedong.iteye.com/blog/1185403 python 2.4 与 python 3.0 的比较 一. print 从语句变为函数 原: pr ...
- 解决maven无法下载依赖的jar包的问题
背景: 公司内部有搭建maven私服,自己做了个核心jar包,一开始是xxx-core.1.0.0.SNAPSHOT版本,是本地和项目环境都可以正常使用的.为支持上线,发布稳定版本,xxx-core. ...
- Ubuntu 16.04通过Snap安装应用程序
16.04LTS可以说是一个不寻常的5年支持版本,同时也带来了Snap应用,并通过Snap可以安装众多的软件包.需要注意的是,Snap是一个全新的软件包架构,但是同样也比其它的软件包大很多. 简单的安 ...
- HDU 4857 topological_sort
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...