hdu 6068--Classic Quotation(kmp+DP)
2018-06-17 21:56:44来源:未知 阅读 ()
题目链接
After doing lots of such things, Little Q finds out that string T occurs as a continuous substring of S′ very often.
Now given strings S and T, Little Q has k questions. Each question is, given L and R, Little Q will remove a substring so that the remain parts are S[1..i] and S[j..n], what is the expected times that T occurs as a continuous substring of S′ if he choose every possible pair of (i,j)(1≤i≤L,R≤j≤n) equiprobably? Your task is to find the answer E, and report E×L×(n−R+1) to him.
Note : When counting occurrences, T can overlap with each other.
In each test case, there are 3 integers n,m,k(1≤n≤50000,1≤m≤100,1≤k≤50000) in the first line, denoting the length of S, the length of T and the number of questions.
In the next line, there is a string S consists of n lower-case English letters.
Then in the next line, there is a string T consists of m lower-case English letters.
In the following k lines, there are 2 integers L,R(1≤L<R≤n) in each line, denoting a question.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long LL; const int N=50005; char s[N],t[105]; int pre[N],num[N][105]; int suf[N][105]; int next1[105]; int next2[105][30],flag[105][30]; int n,m,q; void KMP() { next1[0]=0; for(int i=1,k=0; i<m; ++i) { while(k>0 && t[i]!=t[k]) k=next1[k-1]; if(t[i]==t[k]) k++; next1[i]=k; } } void cal() { memset(flag,0,sizeof(flag)); for(int i=0;i<m;i++) { for(int j=0;j<26;j++) { char x=j+'a'; int k=i; while(k>0 && t[k]!=x) k=next1[k-1]; if(t[k]==x) k++; next2[i][j]=k; if(k==m) flag[i][j]=1,next2[i][j]=next1[m-1]; } } memset(pre,0,sizeof(pre)); memset(num,0,sizeof(num)); for(int i=0,k=0;i<n;i++) { while(k>0&&t[k]!=s[i]) k=next1[k-1]; if(t[k]==s[i]) k++; if(k==m) pre[i]++,num[i][next1[m-1]]=1; else num[i][k]=1; pre[i]+=pre[i-1]; } for(int i=1;i<n;i++) for(int j=0;j<m;j++) num[i][j]+=num[i-1][j]; for(int i=1;i<n;i++) pre[i]+=pre[i-1];///前缀和; memset(suf,0,sizeof(suf)); for(int i=n-1;i>=0;i--) { int x=s[i]-'a'; for(int j=0;j<m;j++) suf[i][j]=flag[j][x]+suf[i+1][next2[j][x]]; } for(int j=0;j<m;j++) ///后缀和; for(int i=n-1;i>=0;i--) suf[i][j]+=suf[i+1][j]; } int main() { int T; cin>>T; while(T--) { scanf("%d%d%d",&n,&m,&q); scanf("%s%s",s,t); KMP(); cal(); while(q--) { int L,R; scanf("%d%d",&L,&R); LL ans=(LL)pre[L-1]*(LL)(n-R+1); for(int i=0;i<m;i++) { ans+=(LL)num[L-1][i]*(LL)suf[R-1][i]; } printf("%lld\n",ans); } } return 0; } /** 2342 8 3 3463 abcababc abc 8 3 234 aabbcccbbb aaabb 4 10 3 23 ababcababc aba 3 5 */
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:孪生素数
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash