【47.92%】【hdu 5763】Another Meaning
生活随笔
收集整理的這篇文章主要介紹了
【47.92%】【hdu 5763】Another Meaning
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1440????Accepted Submission(s): 690
Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to ??. ?? is so smart that he knows the word B in the sentence has two meanings. He wants to know how many kinds of meanings MeiZi can express.
Input The first line of the input gives the number of test cases T; T test cases follow.
Each test case contains two strings A and B, A means the sentence MeiZi sends to ??, B means the word B which has two menaings. string only contains lowercase letters.
Limits
T <= 30
|A| <= 100000
|B| <= |A|
Output For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the number of the different meaning of this sentence may be. Since this number may be quite large, you should output the answer modulo 1000000007.
Sample Input 4 hehehe hehe woquxizaolehehe woquxizaole hehehehe hehe owoadiuhzgneninougur iehiehieh
Sample Output Case #1: 3 Case #2: 2 Case #3: 5 Case #4: 1 HintIn the first case, “ hehehe” can have 3 meaings: “*he”, “he*”, “hehehe”. In the third case, “hehehehe” can have 5 meaings: “*hehe”, “he*he”, “hehe*”, “**”, “hehehehe”.
Author FZU
Source 2016 Multi-University Training Contest 4
【題解】
設c[i]表示前i個字符能組成多少種不同意思。
c[0..lens]初值為1;
c[i] += c[i-1]-1 (不取其特殊意思);
c[i] += c[i-lenb] (如果存在。則取其特殊意思);
abcdefgh
efgh
對于這樣的輸入
c[4]代表的是不取特殊意思的。==1
然后從c[4]可以推到c[4+lenb]
即c[4+lenb]+=c[4];
可以理解為
abcd****
abcdefgh這兩種。
至于怎么取一個合適的位置做這樣的推導。KMP
【代碼】
#include <cstdio> #include <cstring>const int MAX_SIZE = 101000; const int MOD = 1000000007;int t; int f[MAX_SIZE],c[MAX_SIZE]; char s[MAX_SIZE], p[MAX_SIZE]; bool can[MAX_SIZE]; int lens, lenp;void init() {memset(f, 0, sizeof(f));memset(c, 0, sizeof(c));memset(can, false, sizeof(can)); }void input_data() {scanf("%s", s);scanf("%s", p);f[0] = 0; f[1] = 0;lens = strlen(s);lenp = strlen(p);for (int i = 1; i <= lenp - 1; i++){int j = f[i];while (j && (p[i] != p[j])) j = f[j];f[i + 1] = (p[j] == p[i] ? j + 1 : 0);}int j = 0;for (int i = 0; i <= lens - 1; i++){while (j && (p[j] != s[i]))j = f[j];if (p[j] == s[i]) j++;if (j == lenp)can[i - lenp + 1] = true;} }void get_ans() {for (int i = 0; i <= lens; i++)c[i] = 1;for (int i = 0; i <= lens; i++){if (i)c[i] = (c[i] + c[i - 1] - 1) % MOD;if (can[i])c[i + lenp] = (c[i+lenp] + c[i]) % MOD;} }void output_ans() {printf("%d\n", c[lens]); }int main() {//freopen("F:\\rush.txt", "r", stdin);scanf("%d", &t);for (int i = 1; i <= t; i++){printf("Case #%d: ", i);init();input_data();get_ans();output_ans();}return 0; }
轉載于:https://www.cnblogs.com/AWCXV/p/7632243.html
總結
以上是生活随笔為你收集整理的【47.92%】【hdu 5763】Another Meaning的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学生成绩管理系统-程序维护
- 下一篇: HDU-5783 Divide the