Hdu6068 Classic Quotation(2017多校第4场)
来源:互联网 发布:顾比均线的通达信源码 编辑:程序博客网 时间:2024/05/16 11:41
Classic Quotation
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 416 Accepted Submission(s): 139
Problem Description
When online chatting, we can save what somebody said to form his ''Classic Quotation''. Little Q does this, too. What's more? He even changes the original words. Formally, we can assume what somebody said as a string S whose length is n . He will choose a continuous substring of S (or choose nothing), and remove it, then merge the remain parts into a complete one without changing order, marked as S′ . For example, he might remove ''not'' from the string ''I am not SB.'', so that the new string S′ will be ''I am SB.'', which makes it funnier.
After doing lots of such things, Little Q finds out that stringT occurs as a continuous substring of S′ very often.
Now given stringsS 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.
After doing lots of such things, Little Q finds out that string
Now given strings
Note : When counting occurrences,
Input
The first line of the input contains an integer C(1≤C≤15) , denoting the number of test cases.
In each test case, there are3 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 stringS consists of n lower-case English letters.
Then in the next line, there is a stringT consists of m lower-case English letters.
In the followingk lines, there are 2 integers L,R(1≤L<R≤n) in each line, denoting a question.
In each test case, there are
In the next line, there is a string
Then in the next line, there is a string
In the following
Output
For each question, print a single line containing an integer, denoting the answer.
Sample Input
18 5 4iamnotsbiamsb4 73 73 82 7
Sample Output
1100
Source
2017 Multi-University Training Contest - Team 4
————————————————————————————————————
题目的意思是是给出两个字符串s,t,求s字符串截取[1,i][j,n] (1<=i<=l,r<=j<=n)后有多少个子串t;
思路:先贴一下官方题解:
首先通过KMP算法预处理出T的next数组,设:
prefi表示S的前缀i与T进行KMP后KMP的指针到达了哪里。
pregi表示S的前缀i中T出现的次数。
sufi,j表示从S的后缀i,从j指针开始KMP,能匹配多少个T。
那么前缀i拼接上后缀j中T的个数为pregi+sufj,prefi。
令preg为preg的前缀和,suf为suf的后缀和,si,j表示i前面中pref为j的个数,那么对于询问L,R:
ans=∑i=1L∑j=Rnpregi+sufj,prefi=(n−R+1)pregL+∑i=0m−1sL,i×sufR,i
以上所有数组均可以使用KMP和递推求出,时间复杂度O((n+k)m)。
s数组可以在kmp时预处理出来,suf可以通过预处理递推出来,枚举t字符串每一位和a-z匹配算出新的next数组,用dp的方式预处理出suf
具体看代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define mod 10000007#define mem(a,b) memset(a,b,sizeof a)char s[50005],t[505];int nt[505],pren[50005],suf[50005][505],sx[50005][505],mat[505][505],nt2[505][505];void get_next(int x){ nt[0]=0; nt[1]=0; for(int i=2; i<=x; i++) { int k=nt[i-1]; while(k>0&&t[i]!=t[k+1]) k=nt[k]; nt[i]=t[i]==t[k+1]?k+1:0; }}int main(){ int m,n,q,T; for(scanf("%d",&T); T--;) { scanf("%d%d%d",&n,&m,&q); scanf("%s%s",s+1,t+1); get_next(m); memset(sx,0,sizeof sx); memset(pren,0,sizeof pren); for(int i=1,j=0; i<=n; i++) { while(j&&s[i]!=t[j+1]) j=nt[j]; if(s[i]==t[j+1]) j++; pren[i]=pren[i-1]; if(j==m) pren[i]++,j=nt[j]; for(int kk=0; kk<m; kk++) sx[i][kk]=sx[i-1][kk]; sx[i][j]++; } for(int i=1; i<=n; i++) pren[i]+=pren[i-1]; memset(mat,0,sizeof mat); for(int i=0; i<m; i++) for(int j='a'; j<='z'; j++) { int k=i; while(k&&t[k+1]!=j) k=nt[k]; if(t[k+1]==j) k++; if(k==m) k=nt[k],mat[i][j]=1; nt2[i][j]=k; } memset(suf,0,sizeof suf); for(int i=n; i>0; i--) { for(int j=0; j<m; j++) suf[i][j]=mat[j][s[i]]+suf[i+1][nt2[j][s[i]]]; } for(int i=n; i>0; i--) { for(int j=0; j<m; j++) suf[i][j]+=suf[i+1][j]; } while(q--) { int l,r; scanf("%d%d",&l,&r); LL ans=1LL*(n-r+1)*pren[l]; for(int i=0; i<m; i++) { ans+=1LL*sx[l][i]*suf[r][i]; } printf("%lld\n",ans); } } return 0;}
阅读全文
0 0
- Hdu6068 Classic Quotation(2017多校第4场)
- HDU6068 Classic Quotation (KMP)
- HDU6068 Classic Quotation KMP+前缀和
- HDU 6068 Classic Quotation(2017 Multi-University Training Contest 4)
- 2017多校联合第四场/HDU 6068 Classic Quotation(kmp+dp)
- HDU 6068 Classic Quotation
- hdu 6068 Classic Quotation
- HDU 6068 Classic Quotation(kmp+dp)
- quotation
- classic
- indirect quotation & direct quotation
- MY QUOTATION
- 10-quotation
- quotation & citation
- Hdu6075 Questionnaire(2017多校第4场)
- 2017多校第7场
- 2017多校第9场
- 多校第4场1012
- 题目3:统计0~n中有多少个9
- hexo的next主题打赏
- Matlab在最优化计算中的应用
- Qt openGL 使用QImage 生成二维纹理
- hexo的next主题常用设置
- Hdu6068 Classic Quotation(2017多校第4场)
- Java学习第十八天
- netty创建udp客户端
- 函数式编程(一) 认识“编程范式”和“函数”(转自:周见智)
- 题目4:统计0~n中有多少个0,1,2,3,4,5,6,7,8,9
- gradle wrapper
- HDU4565 So Easy!
- centos安装mysql
- 2017 暑假艾教集训 day6