本文版权归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. 持续集成之戏说Check-in Dance(转)

    add by zhj: 先说一下持续集成的定义,这是ThoughtWorks首席科学家Martin Fowler在<持续集成>第二版中给出的,“持续集成是一种软件开发实践.在持续集成中,团 ...

  2. visual studio 2015开发nodejs教程1搭建环境

    http://sailsdoc.swift.ren/ 这里有 sails中文文档 1 安装nodejsv6.10.3 下载地址  https://nodejs.org/dist/v6.10.3/nod ...

  3. Matlab GUI memo

    有一段时间没写博客,一周4篇文章都坚持不下来,不知道写哪个方面的内容,写研究相关就怕论文查重查到,其他方面也没太多时间去学.还是花时间多学点其他方面.废话到此,很早就做过matlab gui相关,现在 ...

  4. Ubuntu 16.04 源码编译安装PHP7+swoole

    备注: Ubuntu 16.04 Server 版安装过程图文详解 Ubuntu16镜像地址: 链接:https://pan.baidu.com/s/1XTVS6BdwPPmSsF-cYF6B7Q 密 ...

  5. (转)VC串口小程序(用SerialPort类)

    ××××××××××××××××××××××××××××××××××××××××××××××××××××× 在MFC里面实现串口通讯有很多方式: 方案一:使用微软公司提供的 串口类,SerialPor ...

  6. bit,byte,char,位,字节,字符 的区别

    bit,byte,char,位,字节,字符 的区别 原创文章,未经作者允许,禁止转载!!!

  7. TWebBrowser静音

    procedure TForm1.FormCreate(Sender: TObject); var   hDSound: Cardinal;   pDirectSoundCreate: Pointer ...

  8. hadoop开发setjar方法

    屏蔽 //job.setJar("/Users/sumeng/IdeaProjects/cloudHadoop/out/artifacts/cloudHadoop_jar/cloudHado ...

  9. 【android】开发笔记---存储篇

    SQLite批量插入数据 当我们执行 db.execSQL("sql语句")的时候,系统进行了一次IO操作,当批量插入成千上万条时,就会消耗掉许多资源. 解决之道是通过事务,统一提 ...

  10. Tomcat源码

    1.Connector   Container:Engine,Host,Context,Wrapper(责任链的设计模式)   Valve: Tomcat 中一个最容易发现的设计模式就是责任链模式,这 ...