codeforces291E Tree-String Problem
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:codeforces291E
正解:哈希+倍增
解题报告:
突然发现我似乎搞复杂了…
不管了,先讲一讲我的做法好了。
因为字符最多只有$3*10^5$个,那么把字符拆成一个个点,构出一张新图,又因为题目中有限制,必须是一条从上往下的路径,
所以对于每个点都考虑以当前点为结尾的字符串是否与要求的相同,这个的话$hash$就可以了,
然后取出一段祖先区间就用倍增维护好了。
做法暴力,好写好想…
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 600011;
const int MAXM = 1200011;
const int mod = 52013147;
const int base = 29;
int n,m,tot,ecnt,father[MAXN],first[MAXN],to[MAXM],next[MAXM],f[MAXN][20],deep[MAXN],ans;
LL mo[MAXN],g[MAXN][20],Ha;
char s[MAXN];
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void init(){
mo[0]=1; for(int i=1;i<=600000;i++) mo[i]=mo[i-1]*base,mo[i]%=mod;
for(int j=1;j<=19;j++)
for(int i=1;i<=tot;i++) {
f[i][j]=f[f[i][j-1]][j-1];
g[i][j]=( g[i][j-1]*mo[(1<<(j-1))] + g[f[i][j-1]][j-1] )%mod;
}
} inline bool check(int x){
LL now=0; int remain=m;
for(int i=19;i>=0;i--)
if(remain>=(1<<i))
now=(now*mo[(1<<i)]+g[x][i])%mod,x=f[x][i],remain-=(1<<i);
if(now==Ha) return true;
return false;
} inline void dfs(int x,int fa){
if(deep[x]>=m) {
if(check(x))
ans++;
}
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
deep[v]=deep[x]+1; dfs(v,x);
}
} inline void work(){
n=tot=getint(); int len,last;
for(int i=2;i<=n;i++) {
father[i]=getint();
scanf("%s",s+1); len=strlen(s+1);
last=father[i];
for(int j=1;j<len;j++) {
tot++;
f[tot][0]=last;
g[tot][0]=s[j]-'a'+1;
link(last,tot);
last=tot;
}
link(last,i);
f[i][0]=last;/*!!!*/
g[i][0]=s[len]-'a'+1;
}
init();
scanf("%s",s+1); m=strlen(s+1);
Ha=0;
for(int i=m;i>=1;i--) Ha=Ha*base+s[i]-'a'+1,Ha%=mod;
dfs(1,0);
printf("%d",ans);
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
codeforces291E Tree-String Problem的更多相关文章
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集
[题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...
- 【HDU3374】 String Problem (最小最大表示法+KMP)
String Problem Description Give you a string with length N, you can generate N strings by left shift ...
- HDOJ3374 String Problem 【KMP】+【最小表示法】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- String Problem hdu 3374 最小表示法加KMP的next数组
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ACM-ICPC2018南京赛区 Mediocre String Problem
Mediocre String Problem 题解: 很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash). 然后剩下的就是要处理以 ...
- hdu3374 String Problem【最小表示法】【exKMP】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 5772 String problem 最大权闭合子图
String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...
随机推荐
- 【使用时发生的意外】HDFS 分布式写入问题 AlreadyBeingCreatedException
进行追加文件时出现AlreadyBeingCreatedException错误 堆栈信息大致如下: org.apache.hadoop.ipc.RemoteException(org.apache.h ...
- Redis应用案例,查找某个值的范围(转)
原文:https://groups.google.com/forum/#!topic/redis-db/lrYbkbxfQiQ 本文来自Redis在Google Group上的一个问题,有一位同学发贴 ...
- GCJ 2015R2(Bilingual-最小割)
Problem C. Bilingual Problem Elliot's parents speak French and English to him at home. He has heard ...
- mysql复习-来源考试
mysql复习- No1 .登录和权限 (一)常用命令1.登录mysqlmysql -h localhost -u root -p 2.重启mysqlservice mysql restart 延 ...
- Redis vs Mongo vs mysql
Redis 和 Mongo 都属于 No-SQL类型的数据库,他们的区别,联系是什么呢?看了一些文章,特总结如下. Redis 最大的特点是,快!为什么快,因为他将大量的东西存储在了memory中.但 ...
- 【spring-boot】spring-boot整合ehcache实现缓存机制
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心 ...
- CentOS 6下OpenCV的安装与配置
自己按照网上的教程一步一步来的 http://www.jb51.net/os/RedHat/280309.html 虚拟机环境 CentOS 6.5 内核版本:4.1.14 64位 gcc,gcc 4 ...
- vim高亮显示文本
行列高亮设置 • 行高亮 " 设置高亮行的颜色,ctermbg设定背景色,ctermfg设定前景色 set cursorline hi CursorLine cterm=NONE cterm ...
- python中变量的交换
a=4 b=5 #第一种 c=0 c=a a=b b=c print('a=%d,b=%d'%(a,b)) #第二种 a=a+b b=a-b a=a-b print('a=%d,b=%d'%(a,b) ...
- JVM内存区域划分Eden Space\Survivor Space\Tenured Gen\Perm Gen
JVM区域分heap区和非heap区. 1)heap区:Eden Space(伊甸园),Survivor Space(幸存者区),Tenured Gen(老年代-养老区). 2)非heap区:Code ...