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 ...
随机推荐
- Palindrome---poj3974(最大回文子串manacher)
题目链接:http://poj.org/problem?id=3974 和hdu上的最长回文题一样,manacher的模板题 #include<stdio.h> #include<s ...
- Apache mahout 源码阅读笔记--DataModel之FileDataModel
要做推荐,用户行为数据是基础. 用户行为数据有哪些字段呢? mahout的DataModel支持,用户ID,ItemID是必须的,偏好值(用户对当前Item的评分),时间戳 这四个字段 {@code ...
- HBase 二次开发 java api和demo
1. 试用thrift python/java以及hbase client api.结论例如以下: 1.1 thrift的安装和公布繁琐.可能会遇到未知的错误,且hbase.thrift的版本 ...
- 设计模式中类的关系UML
在java以及其他的面向对象设计模式中,类与类之间主要有6种关系,他们分别是:依赖.关联.聚合.组合.继承.实现.他们的耦合度依次增强. 1. 依赖(Dependence) 依赖关系的定义为:对于两 ...
- 【云安全与同态加密_调研分析(3)】国内云安全组织及标准——By Me
◆3. 国内云安全组织及标准◆ ◆云安全标准机构(主要的)◆ ◆标准机构介绍◆ ◆相关标准制定◆ ◆建立的相关模型参考◆ ◆备注(其他参考信息)◆ ★中国通信标准化协会(CCSA) ●组织简介:200 ...
- C++继承模型
在C++继承模型中,一个派生类对象表现出来的东西,是其自己的成员加上其基类成员的总和.但这些成员怎样摆放,标准并未强制规定.一般而言,低地址放基类子对象,高地址放派生类对象. 以下从四个部分讨论C++ ...
- PHP json_encode自动转码的问题
用PHP的json_encode处理中文的时候, 中文会被编码成类似于"\u5f20\u4e09"的格式,例如: <?php $arr = array('张三','李四'); ...
- PAT 1148 Werewolf - Simple Version [难理解]
1148 Werewolf - Simple Version (20 分) Werewolf(狼人杀) is a game in which the players are partitioned i ...
- kubernetes Metrics-server 安装
一.Metrics-server安装: 由于使用自动扩容,得安装heapster或metrics-server(由于heapster停止更新故用metrics-server): 1.安装之前需要为ku ...
- mycat 指定mycat节点
mycat 指定节点: /*!mycat:dataNode=order1*/select seq_nextval('APPOINTMENT_NO'); 指定节点创建存储过程或建表: /*!mycat: ...