hdu4622(hash解法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622
Now you are back,and have a task to do:
Given you a string s consist of lower-case English letters only,denote f(s) as the number of distinct sub-string of s.
And you have some query,each time you should calculate f(s[l...r]), s[l...r] means the sub-string of s start from l end at r.
For each test cases,the first line contains a string s(1 <= length of s <= 2000).
Denote the length of s by n.
The second line contains an integer Q(1 <= Q <= 10000),denote the number of queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n), denote a query.
I won't do anything against hash because I am nice.Of course this problem has a solution that don't rely on hash.
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ri register int
typedef long long ll;
typedef unsigned long long ull; inline ll gcd(ll i,ll j){
return j==0?i:gcd(j,i%j);
}
inline ll lcm(ll i,ll j){
return i/gcd(i,j)*j;
}
inline void output(int x){
if(x==0){putchar(48);return;}
int len=0,dg[20];
while(x>0){dg[++len]=x%10;x/=10;}
for(int i=len;i>=1;i--)putchar(dg[i]+48);
}
inline void read(int &x){
char ch=x=0;
int f=1;
while(!isdigit(ch)){
ch=getchar();
if(ch=='-'){
f=-1;
}
}
while(isdigit(ch))
x=x*10+ch-'0',ch=getchar();
x=x*f;
} const ull p=131;
const int maxn=2e3+5;
ull bit[maxn];
char ch[maxn];ull ha[maxn];
const ull mod=1e5+7;
ull getha(int l,int r){
if(l==r)return ch[l];
return ha[r]-ha[l-1]*bit[r-l+1];
}
struct gra{
int head[mod+5];
int pos[maxn];//当前字符出现的最晚的位置
int next[maxn];
ull edg[maxn];//hash值
int num;
void init(){
num=0;
memset(head,0,sizeof(head));
}
int find(ull val,int id){
int u=val%mod;
for(int i=head[u];i!=0;i=next[i]){
if(edg[i]==val){
int tem=pos[i];
pos[i]=id;
return tem;
}
}
num++;
edg[num]=val;
pos[num]=id;
next[num]=head[u];
head[u]=num;
return 0;
}
}h;
int dp[maxn][maxn];
int main(){
int t,q,l,r;
scanf("%d",&t);
bit[0]=1;
for(int i=1;i<maxn;i++){
bit[i]=bit[i-1]*p;
}
while(t--){
scanf("%s",ch+1);
int len=strlen(ch+1);
memset(dp,0,sizeof(dp));
ha[0]=1;
for(int i=1;i<=len;i++){
ha[i]=ha[i-1]*p+ch[i];
}
for(int i=1;i<=len;i++){
h.init();
for(int lm=1;lm+i-1<=len;lm++){
ull tem=getha(lm,lm+i-1);
int pos=h.find(tem,lm);
dp[pos][lm+i-1]--;
dp[lm][lm+i-1]++;
}
}
for(int i=len;i>=1;i--){
for(int j=i+1;j<=len;j++){
dp[i][j]+=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
}
}
scanf("%d",&q);
while(q--){
scanf("%d%d",&l,&r);
printf("%d\n",dp[l][r]);
}
}
return 0;
}
使用map判重:
建图判重:
(区别太大了吧QAQ)
hdu4622(hash解法)的更多相关文章
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- poj 3461 hash解法
字符串hash https://blog.csdn.net/pengwill97/article/details/80879387 https://blog.csdn.net/chaiwenjun00 ...
- SYZOJP186 你猜猜是不是DP 二分+hash解法
SYZOJP186 你猜猜是不是DP题解 题目传送门 现在给两个仅包含小写字母的字符串a,b ,求a 与b的最长公共连续子串的长度. 对于20%的数据,a,b长度 ∈ [1, 200] 对于50%的数 ...
- cf244D. Match & Catch 字符串hash (模板)或 后缀数组。。。
D. Match & Catch 能够用各种方法做.字符串hash.后缀数组,dp.拓展kmp,字典树.. . 字符串hash(模板) http://blog.csdn.net/gdujian ...
- 机试指南第二章-经典入门-Hash的应用自解
Hash的应用: Hash即散列,不像数据结构与算法中讲的各种Hash方法和冲突处理等过多的阐述,以下主要介绍Hash在机试试题解答中的作用. 例2.5 统计同成绩学生人数 Hash解法AC代码:(一 ...
- 51nod(1089 最长回文子串 V2)(hash 加二分)
1089 最长回文子串 V2(Manacher算法) 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度. 输入 ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- k sum 问题系列
转自:http://tech-wonderland.net/blog/summary-of-ksum-problems.html (中文旧版)前言: 做过leetcode的人都知道, 里面有2sum, ...
- HDU-4622 Reincarnation 后缀数组 | Hash,维护和,扫描
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给一个字符串,询问某字串的不同字串的个数. 可以用后缀数组来解决,复杂度O(n).先求出倍 ...
随机推荐
- Windows 上安装 MySQL(8.0.11)
1.接下来我们需要配置下 MySQL 的配置文件 打开刚刚解压的文件夹 C:\web\mysql-8.0.11 ,在该文件夹下创建 my.ini 配置文件,编辑 my.ini 配置以下基本信息: [m ...
- node vue
官网 ECMAScript 6 Node.Js WebPack Vue.js Vuex Vue-loader (类比css-loader,是webpack中用于处理.vue文件的) vue-route ...
- ORA-27302: 错误发生在: sskgpwrm1
opidrv aborting process M002 ospid (3561) as a result of ORA-600ORA-27300: 操作系统相关操作: semctl 失败, 状态为: ...
- 最大化及等比例测试演化Demo-Grid方法
Demo1-简单测试: <!doctype html> <html> <head> <meta charset="utf-8"> & ...
- 思维导图工具XMind下载
XMind 是一款非常实用的商业思维导图软件,全力打造易用.高效的可视化思维软件,强调软件的可扩展.跨平台.稳定性和性能,致力于使用先进的软件技术帮助用户真正意义上提高生产率.XMind 支持 在Wi ...
- 在VS2015中用C++编写可被C#调用的DLL
VS2015用C++创建动态库DLL步骤如下: (1)启动VS2015-->文件-->新建-->项目,按图二进行选择,选择Win32项目,弹出创建窗口,如第二张图.注意.net版本根 ...
- tongweb安装后无法启动问题
安装后执行bin下的启动文件,但是管理界面依然无法打开 1.确认破解的dat文件是否在跟目录下,是否过期 2.如果jdk64位试试下面的方法:JAVA_HOME/jre/lib/ext/下缺少包 su ...
- 【机器学习_8】pandas
背景 关于同一个话题,不同作者也有不同行文结构.但要真正理解并会用,在我的经验里,是必须要自己重新组织的. 本文是基于以往看过的资料,从自身数据处理应用的角度出发,重新组织pandas应用结构,希望能 ...
- ThreadLocal 原理及一些实现
ThreadLocal = TL 网上讲TL原理很多,我大概说下自己的理解 TL其实是不是有点像全局的配置中心,static ConcurrentHashMap<Thread,value> ...
- 修改 Vultr 登录密码
Debian,Ubuntu 访问控制台,打开在线 Console,点击右上角的 “Send CtrlAltDel”,按 ESC 键启动 GRUB boot prompt. 按 e 编辑第一启动项.按 ...