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 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 ...
随机推荐
- 一个好用简单的布局空间EasyUI
之前项目中都是前端来新写的页面,对于很多后台管理系统来说,新写页面其实比较麻烦. 最近看到一款还是不错的开源页面框架EasyUi http://www.jeasyui.com/index.php 这是 ...
- wamp开机自动启动
控制面板--管理工具--服务 将其中的wampapache和wampmysql的属性均改为“自动”即可.
- 开心的金明<0-1背包>
题意:0-1背包经典题: 不多述,直接上代码: 1.二维数组表示法: #include<cstdio> #include<iostream> #include<algor ...
- Android开源项目收集
软件名:gaeproxy软件作用:Android手机配置GoAgentFQ.项目地址:https://github.com/madeye/gaeproxy.git 软件名:ProxyDroid软件作用 ...
- cin 与 scanf 的不同
cin输入更方便: 首先,cin 是个C++类型对象,它的类型是basic_istream,scanf 是个不定参数的函数,其次,cin 所属的类重载了 >> 运算符,使输入更简单了,比如 ...
- 转:检查点(web_reg_find函数详解)
LR检查点 设置检查点的目的不只是为了验证我们的脚本没有错误,而更重要的是一个规范问题,如何使得测试结果更具有说服力,因此建议所有的测试脚本中都添加检查点设置 一.设置检查点的方法 1.将脚本切换到树 ...
- PAT (Advanced Level) 1041. Be Unique (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- Vmware克隆虚拟机后网卡eth0变eth1解决
在克隆虚拟机的过程中,发现新克隆的机器的网卡由eth0变成了eth1,然而并没有eth1的配置文件. 1.#ip a 查看当前ip地址,发现是eth1网卡 2.#ll /etc/sysconfig/n ...
- -linux删除大量文件----rm,rsync
要在linux下删除海量文件,比如有数十万个文件,此时常用的rm -rf * 就会等待时间很长.这时我们可以使用rsync快速删除大量文件. 1.建立一个空目录 mkdir -p /tmp/rsync ...
- 使用maven管理后,依然找不到需要的jar包
使用maven管理后,依然报错,找不到,比如如下错误java.lang.ClassNotFoundException: org.springframework.web.context.ContextL ...