Another Meaning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 266 Accepted Submission(s): 114
Problem Description
As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”.
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.
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|
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
思路:这道题要用dp。dp[i]表示为以i结尾时有多少第一个字符串有多少种含义。然后我们应该怎样dp呢?分两种情况。如果当前位置是能和第二个字符串完全匹配的,也就是说这个地方是可以改变意思的。那么我们有两种状态,一是不改变这里的意思,二是改变这里的意思。不改变意思的方案有dp[i-1]种,改变意思的方案是有dp[i-strlen(s2)]种情况,这个考虑到前面如果改变这里就不能改变的问题就很好理解。显然第二种情况就是不能改变意思,不能改变意思就等于dp[i-1],那么如何判断这里能不能改变意思呢?我们用到了kmp算法标记位置。剩下的也没什么了。。。下面给代码。
#include<iostream>
#include<stack>
#include<cstring>
#include<map>
#include<string>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
#define maxn 100005
#define MOD 1000000007
char s1[maxn], s2[maxn];
int next1[maxn];
int dp[maxn], vis[maxn],tcase;
void makeNext()
{
int q, k;
int m = strlen(s2);
next1[0] = 0;
for (q = 1, k = 0; q < m; ++q)
{
while (k > 0 && s2[q] != s2[k])
k = next1[k - 1];
if (s2[q] == s2[k])
{
k++;
}
next1[q] = k;
}
}
void kmp()
{
int n, m;
int i, q;
n = strlen(s1);
m = strlen(s2);
for (i = 0, q = 0; i < n; ++i)
{
while (q > 0 && s2[q] != s1[i])
q = next1[q - 1];
if (s2[q] == s1[i])
{
q++;
}
if (q == m)
{
vis[i] = 1;
}
}
if (vis[m - 1]){
dp[m - 1] = 2;
}
else
dp[m - 1] = 1;
for (int i = m ; i < n; i++){
if (vis[i]){
dp[i] += max(dp[i - m], 1);
dp[i] %= MOD;
dp[i] += max(dp[i - 1], 1);
dp[i] %= MOD;
}
else{
dp[i] += dp[i - 1];
dp[i] %= MOD;
}
}
printf("Case #%d: %d\n", tcase, dp[n - 1]);
}
int main(){
int t;
scanf("%d", &t);
for (tcase = 1; tcase <= t; tcase++){
scanf("%s%s", s1, s2);
makeNext();
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
kmp();
}
}