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 ...
随机推荐
- nodejs学习笔记<一>安装及环境搭建
零零散散学了几天nodejs,进度一直停滞不前,今天沉下心来好好看了下nodejs的介绍和代码.自己也试着玩了下,算是有点入门了. 这里来做个学习笔记. ——————————————————————— ...
- [js] 前端性能优化
原文链接:http://www.cnblogs.com/xxcanghai/p/5205998.html 链接:http://www.zhihu.com/question/21658448/answe ...
- linux套件安装过程中configure,make,make install的作用
./configure,make,make install都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤.其中: ./configure是检测程序文件,用来检测你的安装平 ...
- iOS开发 判断字符串是不是表情
+ (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; [string enumerateSub ...
- phalcon: acl权限控制
目录控制: public/index.php: $di['aclResource']=function(){ return include_once '../app/config/frontbackA ...
- ios开发者证书 签发者无效
2月14日以后,由于苹果更新安全证书:会导致本机制作的所有开发者证书无效: 钥匙串里的开发者证书无法使用 解决方式: 重新下载苹果公司的安全证书,并安装 1: 先在钥匙串里搜索到老的证书,如果有,请先 ...
- navigator.userAgent.indexOf来判断浏览器类型
navigator.userAgent.indexOf来判断浏览器类型 (2011-03-03 11:30:40) 转载▼ 标签: 杂谈 来源:http://xtaai5233.blog.163. ...
- Completely disable mousewheel on a WinForm
this.MouseWheel += new MouseEventHandler(Form_MouseWheel); private void Form_MouseWheel(object sende ...
- JavaWeb学习-Tomcat
打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:
- meta viewport 详解
ViewPort <meta>标记用于指定用户是否可以缩放Web页面,如果可以,那么缩放到的最大和最小缩放比例是什么.使用ViewPort <meta>标记还表示文档针对移动设 ...