(HDU 5763)Another Meaning <KMP + dp> 多校训练4

Another Meaning
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
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

题意:
给你一个字符串A和B,B可以表示两个意思:B和*,问你A总共可以表示多少个意思?

分析:
首先我们可以利用KMP来标记出A串中所有的匹配的初始位置。
那么每个匹配的位置有两种选择,可以变成*,或者不变
所以就对应着两种状态的转移:
初始化:dp[i] = 0;
我们设dp[i]表示前i个字符可以表示的意思的数目,那么当i不是匹配位置时,那他只能不变,即dp[i+1] += dp[i];
当是匹配的位置时,那么dp[i+Blen]也能从dp[i]状态转移过来,所以dp[i+Blen] += dp[i]。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxn = 100010;
const int mod = 1e9 + 7;
char A[maxn],B[maxn];
int fail[maxn],match[maxn],dp[maxn];
int Alen,Blen;

void getfail()
{
    memset(fail,0,sizeof(fail));
    fail[0] = fail[1] = 0;
    for(int i=1;i<Blen;i++)
    {
        int j = fail[i];
        while(j && B[i] != B[j]) j = fail[j];
        fail[i+1] = B[i] == B[j] ? j+1 : 0;
    }
}

void KMP()
{
    getfail();
    memset(match,0,sizeof(match));
    int j = 0;
    for(int i=0;i<Alen;i++)
    {
        while(j && A[i] != B[j]) j = fail[j];
        if(A[i] == B[j]) j++;
        if(j == Blen)
        {
            match[i-Blen+1] = 1;
            j = fail[j];
        }
    }
}

int main()
{
    int t,cas = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",A,B);
        Alen = strlen(A); Blen = strlen(B);
        KMP();
        memset(dp,0,sizeof(dp));
        dp[0] = 1;
        for(int i=0;i<Alen;i++)
        {
            dp[i+1] = (dp[i+1] + dp[i]) % mod;
            if(match[i]) dp[i + Blen] = (dp[i + Blen] + dp[i]) % mod;
        }
        printf("Case #%d: %d\n",cas++,dp[Alen]);
    }
    return 0;
}
在CUDA编程中,kernel<<<>>>是用来启动并行计算的语法。在<<<>>>中,必须指定一个表达式作为参数,该表达式指定了并行计算的线程块(block)和线程(thread)的数量。这个表达式的格式可以是一个常数,也可以是一个变量或者一个计算表达式。 例如,如果想要启动一个有16个线程块和256个线程的并行计算,可以使用以下形式的表达式: kernel<<<16, 256>>>(); 其中,16表示线程块的数量,256表示每个线程块中的线程数量。 另外,如果希望在编译时指定默认的线程块和线程数量,可以使用宏定义或者模板的方式来实现。通过设置默认值,并在调用kernel时不指定表达式,就可以使用默认的线程块和线程数量。同时,也可以添加依赖关系来根据不同的条件设置不同的默认值。 总结起来,当使用kernel<<<>>>时,必须提供一个表达式来指定线程块和线程的数量。这个表达式可以是一个常数、变量或者计算表达式。另外,也可以通过设置默认值和添加依赖关系来实现更灵活的使用方式。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [玩转CUDA——提示应输入表达式](https://blog.csdn.net/gaohang_hdu/article/details/81119627)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Kconfig语法](https://download.csdn.net/download/pengfei24/4328218)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值