Pty的字符串(string)
题目描述
在神秘的东方有一棵奇葩的树,它有一个固定的根节点(编号为1)。树的每条边上都是一个字符,字符为a,b,c中的一个,你可以从树上的任意一个点出发,然后沿着远离根的边往下行走,在任意一个节点停止,将你经过的边的字符依次写下来,就能得到一个字符串,例如:
在这棵树中我们能够得到的字符串是:
c, cb, ca, a, b, a
现在pty得到了一棵树和一个字符串S。如果S的一个子串[l,r]和树上某条路径所得到的字符串完全相同,则我们称这个子串和该路径匹配。现在pty想知道,S的所有子串和树上的所有路径的匹配总数是多少?
输入格式
第一行:n
接下来n-1行,每行一个整数fa, 一个字符c,字符和整数之间用一个空格隔开
第i行fa代表第i号节点的父亲,c表示第i号节点和fa的连边的字符
最后一行为字符串S
输出格式
输出共一行,表示匹配总数,因为评测系统暂未确定,所以C/C++选手请使用cout输出。
样例
样例输入
5
1 c
2 b
1 a
2 a
cba
样例输出
5
数据范围与提示
【样例说明】
单个字符匹配的对数为4对,两个字符匹配的对数为1对:cb
solution
格式化代码
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 800005
#define ll long long
using namespace std;
int n,id[maxn],rt=,cnt=,la=,a[maxn];
struct node{
int nex[],par,Max,ri;
ll val,sum;
}s[maxn*];
char ch[maxn*];
ll tax[maxn];
void ins(int fa,int c,int i){ int np=++cnt,p=id[fa];s[np].ri=;
//cout<<i<<' '<<c<<' '<<p<<endl;
s[np].Max=s[la].Max+;id[i]=la=np;//这里还不是很懂为什么是la for(;p&&!s[p].nex[c];p=s[p].par)s[p].nex[c]=np;
if(!p)s[np].par=rt;
else {
int q=s[p].nex[c];
if(s[q].Max==s[p].Max+)s[np].par=q;
else {
int nq=++cnt;
for(int i=;i<;i++)s[nq].nex[i]=s[q].nex[i];
s[nq].par=s[q].par;s[nq].Max=s[p].Max+;
s[q].par=s[np].par=nq;
for(;s[p].nex[c]==q;p=s[p].par)s[p].nex[c]=nq;
}
}
}
void Calc(){
for(int i=;i<=cnt;i++)tax[s[i].Max]++;
for(int i=;i<=cnt;i++)tax[i]+=tax[i-];
for(int i=;i<=cnt;i++)a[tax[s[i].Max]--]=i;
for(int i=cnt;i>=;i--){
int k=a[i],f=s[k].par;
s[f].ri+=s[k].ri;
s[k].val=s[k].ri*(s[k].Max-s[f].Max);
}
for(int i=;i<=cnt;i++){
int k=a[i];
s[k].sum=s[k].val+s[s[k].par].sum;
}
}
int main(){
cin>>n;id[]=;
for(int i=,fa;i<=n;i++){
char c;scanf("%d %c",&fa,&c);
ins(fa,c-'a',i);
}
Calc();
scanf("%s",ch);int l=strlen(ch),len=;
int p=rt;ll ans=;
for(int i=;i<l;i++){
for(;!s[p].nex[ch[i]-'a']&&p;p=s[p].par,len=s[p].Max);
if(!p)p=rt,len=;
else {
p=s[p].nex[ch[i]-'a'];len++;
int f=s[p].par;
ans=ans+s[f].sum+(len-s[f].Max)*s[p].ri;
}
}
cout<<ans<<endl;
return ;
}
Pty的字符串(string)的更多相关文章
- javascript类型系统——字符串String类型
× 目录 [1]定义 [2]引号 [3]反斜线[4]特点[5]转字符串 前面的话 javascript没有表示单个字符的字符型,只有字符串String类型,字符型相当于仅包含一个字符的字符串 字符串S ...
- C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)
在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...
- Java常量字符串String理解
Java常量字符串String理解 以前关于String的理解仅限于三点:1.String 是final类,不可继承2.String 类比较字符串相等时时不能用“ == ”,只能用 "eq ...
- 字符串string类型转换成DateTime或DateTime?类型
常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...
- Javascript基础系列之(三)数据类型 (字符串 String)
javascript一共有9种数据类型 字符串 String 数值型 Number 布尔型 Boolean 未定义 Undefine 空值 Null 对象 Object 引用Refernce 列表型 ...
- Java基础——数组应用之字符串String类
字符串String的使用 Java字符串就是Unicode字符序列,例如串“Java”就是4个Unicode字符J,a,v,a组成的. Java中没有内置的字符串类型,而是在标准Java类库中提供了一 ...
- java中字符串String 转 int(转)
java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...
- 字符串String类
1. String类是一个密封类.用关键字sealed修饰: 2. 字符串的两个特性: ·不可变性:string类型变量,一旦声明就表明它是不会被改变的.因此,string中的方法对strin ...
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
随机推荐
- POJ2409 Let it Bead(Polya定理)
Let it Bead Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6443 Accepted: 4315 Descr ...
- Redis连接工具类
Redis连接工具类 导包 测试一下(junit) package com.test; import org.junit.Test; import redis.clients.jedis.Jedis; ...
- Percona-Tookit工具包之pt-sift
Preface We've got a lot of files related with system performance which generated by pt-stalk ...
- pwn的一些环境搭建
<1>pwntools库安装 pwntools是一个CTF框架和漏洞利用开发库,用Python开发,由rapid设计,旨在让使用者简单快速的编写exploit. 本文将基于KUbuntu ...
- jquery横向手风琴效果2
<!doctype html> <html> <head> <meta charset="utf-8"> <script ty ...
- linux c 调用子文件函数
今天在学习初级linux c的时候遇到了如下问题:通过主函数调用同路径下的子文件函数调用失败.博主是这样一一解决的: 首先:hello.c: hello.c: #include<bool.c&g ...
- adnroid 打包问题 :compileReleaseJavaWithJavac
今天打包的时候,由于着急.改了些本地的变化就assembleRelease. 然后就报错: compileReleaseJavaWithJavac 后来网上乱找,.... 之后我想到先跑一下,果然是因 ...
- latex排版系统
proTeXt - MiKTeX-based distribution for Windows proTeXt aims to be an easy-to-install TeX distributi ...
- svn Previous operation has not finished; run 'cleanup' if it was interrupted
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...
- 查询数据库里当前用户下的所有表的总共数据sql
select t.table_name,t.num_rows from user_tables t select sum(num_rows) from user_tables t