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的更多相关文章

  1. hdu 4622 Reincarnation(后缀数组)

    hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...

  2. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

  3. hdu 4622 Reincarnation trie树+树状数组/dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

  4. hdu 4622 Reincarnation SAM模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...

  5. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  6. HDU 4622 Reincarnation(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...

  7. HDU 4622 Reincarnation (查询一段字符串的不同子串个数,后缀自动机)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  8. 字符串(后缀自动机):HDU 4622 Reincarnation

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  9. HDU 4622 Reincarnation 后缀自动机 // BKDRHash(最优hash)

    Reincarnation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) P ...

随机推荐

  1. poj2187Beauty Contest(凸包直径)

    链接 利用旋转卡壳 参考博客http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html #include <iostream> ...

  2. Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace

    Oracle正则表达式函数:regexp_like.regexp_substr.regexp_instr.regexp_replace   --去掉所有特殊字符,只剩字母  SELECT REGEXP ...

  3. access denied ("java.net.SocketPermission" "localhost:1527" "listen,resolve")

    在开启derby服务出现该错误(测试hibernate 连接数据库时  使用myeclipse2014自带的数据库--windows->show view->other->Myecl ...

  4. ssh免密码登陆及其原理

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...

  5. oracle中between

    oracle中between and包含边界值,也就是所谓的闭区间. 如 between 1 and 100,则表示包含1和100及以内的一切数值. 如以下语句: 1 2 3 4 5 6 7 8 9 ...

  6. Freemarker 浅析 (zhuan)

    http://blog.csdn.net/marksinoberg/article/details/52006311 ***************************************** ...

  7. OpenGL的glTexCoord2f纹理坐标配置

    纹理坐标配置函数,先看定义: void glTexCoord2f (GLfloat s, GLfloat t); 1.glTexCoord2f()函数 有两个参数:GLfloat s, GLfloat ...

  8. wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问。

    wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问. 因为做项目,多人分工,需要局域网内访问各自的项目. 然后安装了wampserver2.5,Ap ...

  9. vitamio 缓冲一部分时,loading还没消失,直接点击播放,loading未能消失

    在videoView的start()中 添加loading消失语句,效果很好 ,也没有出现任何问题

  10. linux 下 `dirname $0`

    [`],学名叫“倒引号”, 如果被“倒引号”括起来,  表示里面需要执行的是命令.比如 `dirname $0`,  就表示需要执行   dirname $0  这个命令 [“”] , 被双引号括起来 ...