hdu 4622 Reincarnation
http://acm.hdu.edu.cn/showproblem.php?pid=4622
用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上
把所用的串对应的整数放在一个数组里 比如书字符串s[l...r]对应的整数是 k
那么二维数组 [l][r] 就等于k
假设一个对应好的二维数组 左下角是原点
3 4 5 2
2 3 4 0
1 6 0 0
2 0 0 0
这样求解 从l到r的不同字符串的个数 其实就是求 从[l][r] 到右下角所在的矩阵所包含不同整数的个数(不包括0)
这里需要一定的去重处理 处理后是
-1 0 1 1
0 1 1 0
1 1 0 0
1 0 0 0
然后一边dp就可以求出所有答案
注意常用的next[26]写法的字典树有可能超内存 要优化
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const double eps = (1e-9);
const int N=2005;
const int M=2000000;
const int K=27;
char s[N];
int c[N][N];
int last[M];
struct node
{
int head;
int k;
}trie[M];
struct node1
{
int j,next;
char c;
}edge[M];
int num,cnt,I;
int add(char a,int &x)
{
bool flag=false;
for(int t=trie[x].head;t!=-1;t=edge[t].next)
{
if(edge[t].c==a)
{flag=true;x=edge[t].j;break;}
}
if(flag==true)
return trie[x].k;
trie[cnt].head=-1;
trie[cnt].k=num++;
edge[I].c=a;
edge[I].j=cnt;
edge[I].next=trie[x].head;
trie[x].head=I++;
x=cnt++;
return trie[x].k;
}
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
trie[0].head=-1;
trie[0].k=-1;
I=0;num=1,cnt=1;
memset(c,0,sizeof(c));
memset(last,-1,sizeof(last));
scanf(" ");
gets(s);
int n=strlen(s);
for(int j=0;j<n;++j)
for(int i=j,t=0;i>=0;--i)
{
int x=i+1;
int y=j+1;
int k=add(s[i],t);
if(last[k]==-1)
{
last[k]=x;
++c[x][y];
}else if(last[k]<x)
{
++c[x][y];
--c[last[k]][y];
last[k]=x;
}
}
for(int j=1;j<=n;++j)
for(int i=n;i>=1;--i)
c[i][j]+=c[i][j-1]+c[i+1][j]-c[i+1][j-1];
int q;
scanf("%d",&q);
while(q--)
{
int x,y;
scanf("%d %d",&x,&y);
printf("%d\n",c[x][y]);
}
}
return 0;
}
hdu 4622 Reincarnation的更多相关文章
- hdu 4622 Reincarnation(后缀数组)
hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- hdu 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- hdu 4622 Reincarnation SAM模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
- HDU 4622 Reincarnation(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...
- HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- 字符串(后缀自动机):HDU 4622 Reincarnation
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) P ...
随机推荐
- HDU3247 AC自动机+dp
题意:给出n个资源,m个病毒,将资源串拼接成一个串,必须包含所有的资源串,可以重叠,但是不能包含病毒,问最小的长度为多少 题解:所有串建AC自动机.对以资源串结尾的结点跑bfs,求出到其他资源串结尾的 ...
- 02 key concept
本章提要-------------------------------------术语, 选择性与基数, 软解析与硬解析, 绑定变量, 扩展的游标共享SQL语句生命周期, 特别关注解析部分------ ...
- (转)使用Jmeter进行http接口测试
前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对 ...
- SQL SERVER 2008函数大全(含例子)
--SQL SERVER 2008 函数大全 /* author:TracyLee csdncount:Travylee */ /* 一.字符串函数: 1.ascii(字符串表达式) 返回字符串 ...
- 什么是Plist文件
直接将数据直接写在代码里面,不是一种合理的做法.如果数据经常改,就要经常翻开对应的代码进行修改,造成代码扩展性低 因此,可以考虑将经常变的数据放在文件中进行存储,程序启动后从文件中读取最新的数据.如果 ...
- gO语言的安装和环境变量的配置
一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更深层次研究go语言. 二.G ...
- maven实战_01_搭建maven开发环境
一 下载maven 在maven官网上可下载maven:http://maven.apache.org/download.cgi 下载好后,解压.我的解压到了:D:\maven\apache-mave ...
- Oracle Names - Oracle_SID /db_name instance_name service_names / service_name / sid / sid_name
很多人还是困惑,下面再次尝试从几个不同角度区分一下: Oracle_SID / db_name, instance_name, service_names / service_name, sid / ...
- WebBrowser的内存释放
WebBrowser窗口自动滚动: this.webBrowser.Document.Window.ScrollTo(0, webBrowser1.Document.Body.ScrollRectan ...
- hiho1092_have lunch together
题目 两个人从同一个点出发,在一个餐厅中寻找两个相邻的座位,需要是的从出发点到达座位的距离总和最短.题目链接: Have Lunch Together 最短路程,一开始以为要用dijkstra ...