Poi2006 Palindromes
2780: Poi2006 Palindromes
Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 15 Solved: 5
[Submit][Status][Web Board]
Description
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.
给出n个回文串s1, s2, …, sn
求如下二元组(i, j)的个数
si + sj 仍然是回文串
规模
输入串总长不超过2M bytes
Input
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
Sample Input
3
1 a
2 ab
2 ba
Sample Output
5
//The 5 palindromes are:aa aba aba abba baab
HINT
Source
题解:
这个怎么写呢,一脸懵比。。。。。。。
(龙老师说:)我们先把所有串插入一个trie树,然后统计每个串的hash,然后再枚举每个串,沿trie树向下走,枚举每一个前缀,判断他们俩连起来的字符串正着和 反着是否一样,
直接hash判断即可。
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define maxn 2000000
#define base 199
using namespace std;
int n,k;
ll hashmod,ans,tot;
ll h[maxn],ha[maxn];
int son[maxn][],sum[maxn],f[maxn],len[maxn];
string s[maxn],ch;
int main()
{
h[]=;
for (int i=; i<=maxn+; i++) h[i]=h[i-]*base;
cin>>n;
ans=-n;
for (int i=; i<=n; i++)
{
cin>>len[i];
cin>>ch;
s[i]=' '+ch;
int p=; hashmod=;
for (int j=; j<=len[i]; j++)
{
int x=s[i][j]-'a'+;
if (!son[p][x]) son[p][x]=++tot;
p=son[p][x];
hashmod=hashmod*base+x;
}
//cout<<hashmod<<endl;
ha[i]=hashmod;
f[p]=i; sum[p]++;
}
//cout<<tot;
for (int i=; i<=n; i++)
{
int p=;
for (int j=; j<=len[i]; j++)
{
int x=s[i][j]-'a'+;
p=son[p][x];
// cout<<p<<endl;
//if (sum[p] && ha[f[p]]*h[len[i]]+ha[i]==ha[i]*h[j+1]+ha[f[p]]) ans+=(ll)sum[p]*2;
if(son[p]&&ha[f[p]]*h[len[i]]+ha[i]==ha[i]*h[j]+ha[f[p]])ans+=(ll)sum[p]*;
}
}
printf("%lld\n",ans); return ;
}
Poi2006 Palindromes的更多相关文章
- BZOJ1524: [POI2006]Pal
1524: [POI2006]Pal Time Limit: 5 Sec Memory Limit: 357 MBSubmit: 308 Solved: 101[Submit][Status] D ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- hdu 1318 Palindromes
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- Dual Palindromes
Dual PalindromesMario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same ...
- ytu 1940:Palindromes _easy version(水题)
Palindromes _easy version Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 47 Solved: 27[Submit][Statu ...
- 回文串+回溯法 URAL 1635 Mnemonics and Palindromes
题目传送门 /* 题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案 回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数 ...
- UVA 11584 一 Partitioning by Palindromes
Partitioning by Palindromes Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes
P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 ...
随机推荐
- Magento首页显示产品
Magento首页显示产品 经常用的比较琐碎,上官网发现一个稍微全一点的不过没有针对 具体使用过程中遇到的情况进行修正 这边只做一个备忘吧 (细节问题 按个别情况进行修改即可) New ...
- 关于DOMContentLoaded
也许还有朋友不太清楚DOMContentLoaded这个事件.简单的说,这个事件就是要在大多数情况下去替代window.onload事件,因为window.onload事件必须要等待页面所有元素的资源 ...
- java邮件收发
http://blog.csdn.net/ycg01/article/details/1394465 java邮件收发 标签: javaimportexceptionnulluserclass 200 ...
- hdu_2844_Coins(多重背包)
题目连接:hdu_2844_Coins 题意:给你n个硬币的价值和对应的数量,问你从1到m有那些数能组合出来 题解:如果我们将硬币的价值看成一个物品的容量和价值,那么对应1-m,如果dp[i]==i, ...
- 如何在Apache中配置多端口访问
环境: Windows server 2008 R2, Apache, PHP5 步骤: 建立一个目录,里面放置一个index.php. 打开Apache\conf\httpd.conf 文件,做如下 ...
- STM32软件复位(基于库文件V3.5)
源:STM32软件复位(基于库文件V3.5) void SoftReset(void) { __set_FAULTMASK(); // 关闭所有中端 NVIC_SystemReset();// 复位 ...
- myeclipse中打开java文件中文乱码
中文乱码肯定是编码与解码不一样导致. 1.如果是平时写代码都没有问题,但是打开其他项目时出现这种问题: window->preferences->General->Content T ...
- 介绍Angular的注入服务
其实angular的注入服务是挺复杂的,目前看源码也只看懂了一半,为了不误导大家,我也不讲敢讲太复杂,怕自己都理解错了. 首先我们要知道angular的三种注入方式: 第一种:inference va ...
- PAT (Advanced Level) 1020. Tree Traversals (25)
递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...
- php的几个内置的函数
// 取得所有的后缀为PHP的文件 $files = glob(‘*.php’); print_r($files); j