CSU-ACM2017暑期训练12-KMP H - 结合dp

H - 结合dp

 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

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”.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 1e5+10, mod = 1000000007;

char s[maxn], ss[maxn];
int dp[maxn], Next[maxn], len1, len2;
bool vis[maxn];

void getNext(){
    int len = len2;
    Next[0] = Next[1] = 0;
    int j;
    for(int i = 1; i < len; i++){
        j = Next[i];
        while(j && s[j] != s[i])
            j = Next[j];
        if(s[j] == s[i])
            Next[i + 1] = j + 1;
        else
            Next[i + 1] = 0;
    }
}

void KMPSearch(){
    getNext();
    int len = len1, m = len2;
    int j = 0;
    for(int i = 0; i < len; i++){
        while(j && ss[i] != s[j])
            j = Next[j];
        if(ss[i] == s[j])
            j++;
        if(j == m)
            vis[i+1] = true;
    }
}

int main(){
#ifdef TEST
freopen("test.txt", "r", stdin);
#endif // TEST

    int T, Case = 1;
    cin >> T;
    while(T--){
        memset(dp, 0, sizeof(dp));
        memset(vis, false, sizeof(vis));
        scanf("%s%s", ss, s);
        len1 = strlen(ss); len2 = strlen(s);
        KMPSearch();
        dp[0] = 1;
        for(int i = 1; i <= len1; i++){
            dp[i] = dp[i - 1];
            if(vis[i])
                dp[i] = (dp[i] + dp[i - len2]) % mod;
        }
        printf("Case #%d: %d\n", Case++, dp[len1]);
//        cout << dp[len1] << endl;
    }

    return 0;
}
根据提供的引用内容,SPPFCSPC是一种空间金字塔池化改进的方法。下面是SPPFCSPC的代码详解: ```python import torch import torch.nn as nn class SPPFCSPC(nn.Module): def __init__(self, in_channels, out_channels): super(SPPFCSPC, self).__init__() self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1) self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2) self.spp = SpatialPyramidPooling(out_channels) self.fc = nn.Linear(out_channels * 4, out_channels) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.maxpool(x) x = self.spp(x) x = x.view(x.size(0), -1) x = self.fc(x) return x class SpatialPyramidPooling(nn.Module): def __init__(self, out_channels): super(SpatialPyramidPooling, self).__init__() self.out_channels = out_channels def forward(self, x): pool_sizes = [4, 2, 1] # 池化层的大小 output = [] for pool_size in pool_sizes: pool = nn.AdaptiveMaxPool2d(pool_size) pooled = pool(x) output.append(pooled) output = torch.cat(output, dim=1) return output # 使用示例 in_channels = 3 out_channels = 64 input_data = torch.randn(1, in_channels, 224, 224) model = SPPFCSPC(in_channels, out_channels) output = model(input_data) print(output.shape) ``` 上述代码中,`SPPFCSPC`是一个继承自`nn.Module`的自定义模型,它包含了卷积层、最大池化层、空间金字塔池化层和全连接层。`SpatialPyramidPooling`是一个单独的模块,用于实现空间金字塔池化操作。 在`SPPFCSPC`的前向传播过程中,输入数据经过两个卷积层和一个最大池化层,然后通过空间金字塔池化层进行特征提取,最后通过全连接层输出最终结果。 使用示例中,我们创建了一个输入数据`input_data`,它的形状是`(1, in_channels, 224, 224)`,然后创建了一个`SPPFCSPC`模型,并将输入数据传入模型进行前向传播,得到输出结果`output`。最后打印输出结果的形状。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值