hdu 6153 A Secret KMP
来源:互联网 发布:同花顺金融数据录入 编辑:程序博客网 时间:2024/06/05 02:45
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Problem Description
Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,which have a big secret.SF is interested in this secret and ask VS how to get it.There are the things that VS tell:
Suffix(S2,i) = S2[i...len].Ni is the times that Suffix(S2,i) occurs in S1 and Li is the length of Suffix(S2,i).Then the secret is the sum of the product of Ni and Li.
Now SF wants you to help him find the secret.The answer may be very large, so the answer should mod 1000000007.
Suffix(S2,i) = S2[i...len].Ni is the times that Suffix(S2,i) occurs in S1 and Li is the length of Suffix(S2,i).Then the secret is the sum of the product of Ni and Li.
Now SF wants you to help him find the secret.The answer may be very large, so the answer should mod 1000000007.
Input
Input contains multiple cases.
The first line contains an integer T,the number of cases.Then following T cases.
Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.
1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.
The first line contains an integer T,the number of cases.Then following T cases.
Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.
1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.
Output
For each test case,output a single line containing a integer,the answer of test case.
The answer may be very large, so the answer should mod 1e9+7.
The answer may be very large, so the answer should mod 1e9+7.
Sample Input
2aaaaaaaabababababa
Sample Output
1319Hintcase 2: Suffix(S2,1) = "aba",Suffix(S2,2) = "ba",Suffix(S2,3) = "a".N1 = 3,N2 = 3,N3 = 4.L1 = 3,L2 = 2,L3 = 1.ans = (3*3+3*2+4*1)%1000000007.
Source
2017中国大学生程序设计竞赛 - 网络选拔赛
S,T先逆序处理后,相当于统计T中所有前缀t在S中出现的次数;
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.Arrays;import java.util.StringTokenizer;public class Main { public static void main(String[] args) { new Task().solve(); }}class Task { InputReader in = new InputReader(System.in) ; PrintWriter out = new PrintWriter(System.out) ; final long Mod = 1000000007L ; final int maxn = 1000008 ; int n,m; char[] a = new char[maxn] ; char[] b = new char[maxn] ; int[] p = new int[maxn] ; int[] dp = new int[maxn] ; void getp(){ p[1]=0; int i,j=0; for(i=2;i<=m;i++){ while(j>0&&b[j+1]!=b[i]){ j=p[j]; } if(b[j+1]==b[i]){ j+=1; } p[i]=j; } } void kmp(int cnt[]){ int i,j=0; for(i=1;i<=n;i++){ while(j>0&&b[j+1]!=a[i]){ j=p[j]; } if(b[j+1]==a[i]){ j+=1; cnt[j]++; } if(j==m){ j=p[j]; } } for(i=m;i>=1;i--){ if(p[i]!=0){ cnt[p[i]]+=cnt[i]; } } } void reverse(char s[] , int len){ for(int i=1;i<=len/2;i++){ char t = s[i] ; s[i] = s[len-i+1] ; s[len-i+1] = t ; } } void solve(){ int t = in.nextInt() ; while(t-- > 0){ String s = in.next() ; n = s.length() ; for(int i = 1 ; i <= n ; i++){ a[i] = s.charAt(i-1) ; } s = in.next() ; m = s.length() ; for(int i = 1 ; i <= m ; i++){ b[i] = s.charAt(i-1) ; } reverse(a , n); reverse(b , m); Arrays.fill(dp, 0) ; getp(); kmp(dp); long sum = 0 ; for(int i = 1 ; i <= m ; i++){ sum += ((long)dp[i] * i % Mod) ; sum %= Mod ; } out.println(sum) ; } out.flush() ; }}class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = new StringTokenizer(""); } private void eat(String s) { tokenizer = new StringTokenizer(s); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextInts(int n) { int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = nextInt(); } return nums; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } }
阅读全文
0 1
- hdu 6153 A Secret KMP
- HDU 6153 A Secret KMP
- HDU 6153 A Secret KMP
- Hdu 6153 A Secret【KMP】
- 【KMP】HDU 6153 A Secret
- HDU 6153 A Secret(KMP)
- hdu 6153 A Secret (kmp)
- hdu 6153 A Secret (KMP)
- hdu 6153 A Secret(KMP)
- hdu 6153 A Secret(KMP)
- HDU 6153 A secret(kmp)
- HDU 6153 A Secret【KMP||扩展KMP】
- hdu 6153 A Secret KMP&&扩展KMP
- HDU 6153-A Secret(kmp&&ccpc)
- HDU 6153 A Secret(KMP)
- HDU 6153 A Secret (扩展KMP)
- HDU 6153A Secret(kmp)
- HDU 6153 A Secret 经典扩展KMP
- 神经网络中常见的激活函数
- Python知识点笔记之语法
- Codeforces Round #429 (Div. 2) | A. Generous Kefa
- 我的个人提升计划
- Java集合-类的继承组合关系
- hdu 6153 A Secret KMP
- 8月19日小结
- 对3 5 7 取不同模的结果
- Java集合-ArrayList深入浅出源码分析
- shell易错点
- NLTK学习之四:文本信息抽取
- shell脚本
- zsh: command not found: conda
- 从u盘启动树莓派3