多校4 Another Meaning 5763

来源:互联网 发布:甜甜圈烤机软件 编辑:程序博客网 时间:2024/05/22 06:40

Another Meaning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1266    Accepted Submission(s): 595


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.
 

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
4hehehehehewoquxizaolehehewoquxizaoleheheheheheheowoadiuhzgneninouguriehiehieh
 

Sample Output
Case #1: 3Case #2: 2Case #3: 5Case #4: 1
Hint
In 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”.
 


题意:给一a串给一b串,问有几种匹配方式?(可匹配可不匹配)

思路:第一个字符匹配上了那后面Nb-1个字符都必须匹配,第一个字符没匹配上,后面的字符可匹配可不匹配,所以前后是有关系的,用DP,分两种情况匹配上没匹配上,两种情况又分别分为后面匹配上和后面没匹配上,列递推式,普通匹配极限数据会超时(虽然当时没超...)所以匹配加个kmp或哈希


代码:

#include <cstdio>#include <cstring>#include <string>#include <iostream>#include <cmath>#include <map>#define mod 1000000007using namespace std;const unsigned long long B = 1e8+7;    /*mod*/const int MAXN = 100000+100;char a[MAXN],b[MAXN],tmp[MAXN];long long dp[MAXN];bool vis[MAXN];void hashfind(){    int al=strlen(a),bl=strlen(b);    if(al>bl)    {        strcpy(tmp,a);        strcpy(a,b);        strcpy(b,tmp);    }    unsigned long long t=1,ah=0,bh=0;    for(int i=0; i<al; i++)    {        t*=B;        ah=ah*B+a[i];        bh=bh*B+b[i];    }    for(int i=0; i+al<=bl; i++)    {        if(ah==bh)vis[i]=1;                       //匹配到的坐标        if(i+al<bl)bh=bh*B+b[i+al]-b[i]*t;    }//    return ans;}int main(){    int t;    cin>>t;    int cas = t;    while(t--)    {        scanf("%s%s",b,a);        memset(vis,0,sizeof vis);        hashfind();        int m = strlen(b),n = strlen(a);        for(int i = 0; i <= m; i++)            dp[i] = 1;        for(int i = m-n; i >=0; i--)        {            if(vis[i])                dp[i] = (dp[i+n] + dp[i+1])%mod;                      else                dp[i] =  dp[i+1]%mod;        }        cout<<"Case #"<<cas-t<<": "<<dp[0]<<endl;    }    return 0;}






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 成都怎么办5年居住证 换单位了住房公积金怎么办 单位不交住房公积金怎么办 居转户没有离职证明怎么办 外地户口审驾照怎么办 居住证被注销了怎么办 新到上海怎么办居住证 战网积分过期怎么办 代理一年无赢利怎么办 开庭后不判决怎么办 离婚判决书没了怎么办 去英国工作签证怎么办 在美国怎么办英国签证 换护照英国签证怎么办 英国签证前咳嗽怎么办 英国留学被退学怎么办 在澳洲怎么办韩国签证 美签迟到了怎么办 签证照片贴错怎么办 过隧道耳朵难受怎么办 跑货车没货源怎么办 改文职老职工怎么办 铁路办家属证怎么办? 辐射4电梯故障怎么办 车辆被恶意损坏怎么办 汽车划伤见底怎么办 汽车被刀片划伤怎么办 汽车被笔画了怎么办 车被划了一条线怎么办 微信附近人上门被骗怎么办 交通事故认定书不服怎么办 自动挡下坡刹车失灵怎么办 自动挡汽车刹车失灵怎么办 重车刹车失灵怎么办 12306买票待核验怎么办 单位分流不想去怎么办 公司降薪不同意怎么办 浙江违章扣分太多怎么办 船员进出青岛港怎么办 顶楼供暖不热怎么办 成都地铁掉东西怎么办