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 ...
随机推荐
- HDU5845 Best Division
递归写法,好久不写很容易就gg了... dp[i]=max(dp[j])+1,并且s[i]XORs[j]<=x 01字典树优化一下转移. #include <bits/stdc++.h& ...
- SQL & PL/SQL 模块总结
SQL 1. 各种function 2. merge 3. connect by PL/SQL 1. pl/sql 寄出 2. 游标 3. procedure 4. function 5. packa ...
- Python学习(17)异常处理
目录 Python 异常处理 Python 标准异常 异常处理 使用except而不带任何异常类型 使用except而带多种异常类型 try-finally 语句 异常参数 异常的参数 用户自定义参数 ...
- Android广播BroadcastReceiver 一
Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...
- [css] px em rem
一.区别 px是相对于显示器屏幕分辨率而言的. em相对于浏览器的默认字体尺寸. rem相对于HTML根元素. 二.使用 1.em 任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: ...
- LCA模板
/*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca ...
- MapReduce中作业调度机制
MapReduce中作业调度机制主要有3种: 1.先入先出FIFO Hadoop 中默认的调度器,它先按照作业的优先级高低,再按照到达时间的先后选择被执行的作业. 2.公平调度器(相当于时间 ...
- PV UV IP含义及区别
--------首先来看看ip.uv和pv的定义---------- PV(访问量):即Page View, 即页面浏览量或点击量,用户每次刷新即被计算一次. UV(独立访客):即Unique Vis ...
- 有用的dede表单代码
<form action="" class="demoform"> <table> ...
- OneProxy读写分离配置操作手册
1.确保已配置好主备集群 A)配置 可参考MySQL官方文档(https://dev.mysql.com/doc/refman/5.6/en/replication-howto.html) 或者我的博 ...