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. poj1319Pipe Fitters

    链接 算不上几何的水题 第一种为(int)a*(int)b: 第二种分宽高交换两种讨论. 每一个的高度除第一个为1外其它的都可以看着b/sqrt(3.0)/2; #include <iostre ...

  2. Linux之free命令

    from http://www.cnblogs.com/peida/archive/2012/12/25/2831814.html free命令可以显示Linux系统中空闲的.已用的物理内存及swap ...

  3. surfaceview介绍

    [1]surfaceview 控件是一个重量级控件      [2]内部维护了2个线程     A 获取数据  负责显示     B 负责显示  获取数据      [3]他可以直接在子线程更新ui ...

  4. iOS开发之Xcode 6更新默认不支持armv7s架构

    最近一次的Xcode 6更新默认不再支持arm7s架构,究竟是要废除不用呢还是仅仅只是一个疏忽? 目前的Xcode 6配置里定义${ARCHS_STANDARD}为armv7, arm64,当然这个定 ...

  5. [js] 前端性能优化

    原文链接:http://www.cnblogs.com/xxcanghai/p/5205998.html 链接:http://www.zhihu.com/question/21658448/answe ...

  6. CSS3_边框属性之圆角的基本图形案例

    一.正方形 div{ background:#F00; width:100px; height:100px;}   二.长方形 div{background:#F00;width:200px;heig ...

  7. RMAN-03009 ORA-19504 ORA-27038

    错误信息如下: RMAN> backup database tag='full20160112' format '/orabak/rman/full20160112' include curre ...

  8. MVC HtmlHelper用法大全

    MVC HtmlHelper用法大全HtmlHelper用来在视图中呈现 HTML 控件.以下列表显示了当前可用的一些 HTML 帮助器. 本主题演示所列出的带有星号 (*) 的帮助器. ·Actio ...

  9. hdu_2089 不要62

    数位动态规划     数位动态规划是求解一个大区间[L, R]中间满足条件Q的所有数字的个数(或者和,或其他)的一种方法.它通过分析每一位上的数字,一般用 dp[len][digit][...] 来表 ...

  10. is(C# 参考)

    检查对象是否与给定类型兼容. 例如,下面的代码可以确定对象是否为 MyObject 类型的一个实例,或者对象是否为从 MyObject 派生的一个类型:     if (obj is MyObject ...