本文版权归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的更多相关文章

  1. hdu String Problem(最小表示法入门题)

    hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...

  2. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集

    [题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...

  4. 【HDU3374】 String Problem (最小最大表示法+KMP)

    String Problem Description Give you a string with length N, you can generate N strings by left shift ...

  5. HDOJ3374 String Problem 【KMP】+【最小表示法】

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  7. String Problem hdu 3374 最小表示法加KMP的next数组

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. ACM-ICPC2018南京赛区 Mediocre String Problem

    Mediocre String Problem 题解: 很容易想到将第一个串反过来,然后对于s串的每个位置可以求出t的前缀和它匹配了多少个(EXKMP 或者 二分+hash). 然后剩下的就是要处理以 ...

  9. hdu3374 String Problem【最小表示法】【exKMP】

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. hdu 5772 String problem 最大权闭合子图

    String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...

随机推荐

  1. Json与字符串互相转换

    jQuery插件支持的转换方式:    $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象      浏览器 ...

  2. Python的编码问题(一)

    一.什么是编码 可以说,计算机是一个即聪明又笨蛋的家伙.说它聪明,是因为他可以做很多事情,它的强大无需多说,大家应该都有所了解以及感受.但是为什么说它又是个笨蛋呢,因为我们在电脑上写出的每一个字,保存 ...

  3. git学习------>在CenterOS系统上安装GitLab并自定义域名访问GitLab管理页面

    目前就职的公司一直使用SVN作为版本管理,现在打算尝试从SVN迁移到Git.安排我来预言并搭建好相关的环境以及自己尝试使用Git.今天我就尝试在Center OS系统上安装GitLab,现在在此记录一 ...

  4. (2.5)Mysql之SQL基础——数据类型

    (2.5)Mysql之SQL基础——数据类型 关键词:mysql数据类型 目录: 一.整数型 二.小数型(以下均不能使用无符号) 三.日期时间型 四.字符串型 一.整数型 额外参数示例: int [( ...

  5. PageHelper中默认PageInfo成员变量

    以下是PageHelper中默认PageInfo的成员变量,方便以后自己使用 //当前页 private int pageNum; //每页的数量 private int pageSize; //当前 ...

  6. FileZilla使用

    FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版本,具备所有的FTP软件功能.可控性.有条理的界面和管理多站点的简化方式使得Filezilla客户端版成为一个方便高效的FTP客户 ...

  7. 把当前文件夹的xlsx或xls文件合并到一个excel文件中的不同sheet中

    把当前文件夹的xlsx或xls文件合并到一个excel文件中的不同sheet中步骤如下: 把需要合并的文件放到同一个文件夹 在该文件夹中新建一个excel文件 打开新建的excel问价,把鼠标放到sh ...

  8. 工作笔记——限定input上传文件对话框中能选取的文件的格式

    原文:http://www.dengzhr.com/frontend/1059 input[file]标签的accept属性可用于指定上传文件的 MIME类型 . 例如,想要实现默认上传图片文件的代码 ...

  9. Django restframwork实现自定义数据格式的分页与搜索

    最近因为在做分页时遇到的问题很多,页浪费了好多时间,所以记录一下.以后如遇到可用省去不必要的麻烦 restframwork中的官方文档对分页和搜索页进行了详细的介绍,但是我公司需要的return的js ...

  10. 48. Rotate Image(旋转矩阵)

      You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise) ...