HDUPattern and Text 枚举

这题不要去考虑每一个时刻的情况,而要考虑每一个位在所有时间上的叠加效果。

对于长度为Lf的母串和长度为Ls的子串来说,每个字符匹配的长度都是Lf-Ls+1位。

代码如下:

#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;

char f[2000005], s[2000005];

int lens, lenf, cnt[30];

int main()
{
    int T, LIM;
    long long int ans;
    scanf("%d", &T);
    while (T--) {
        ans = 0;
        memset(cnt, 0, sizeof (cnt));
        scanf("%s %s", s, f);
        lenf = strlen(f);
        lens = strlen(s);
        LIM = lenf - lens + 1;
        for (int i = 0;  i < LIM; ++i) {
            ++cnt[f[i]-'a'];
        }
        ans += cnt[s[0]-'a'];
        for (int i = LIM; i < lenf; ++i) {
            --cnt[f[i-LIM]-'a'];
            ++cnt[f[i]-'a'];
            ans += cnt[s[i-LIM+1]-'a'];
        }
        printf("%I64d\n", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Lyush/p/2617714.html