Gym - 101972H Beautiful Substrings 思維 + 字尾和
You are given two strings a and b consisting of lowercase English letters. A beautiful substring is defined as a substring of any length of string b such that the first and last letters of it are the same as the first and last letters of any substring of length k of string a
Your task is to count the number of beautiful substrings. Can you?
Input
The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.
The first line of each test case contains three integers n, m, and k (1 ≤ n, m ≤ 105, 1 ≤ k ≤ n), in which n
Then two lines follow, the first line contains a string a of length n and the second line contains a string b of length m. Both strings consist only of lowercase English letters.
Output
For each test case, print a single line containing the number of beautiful substrings
Example
Input
2 4 5 3 acbd abcbd 3 3 1 kkd dkd
Output
3 4
Note
A substring of a string s is a sequence sl, sl + 1, ..., sr for some integers (l, r)such that (1 ≤ l ≤ r ≤ n), in which n is the length of the string s.
題意:給定兩個字串,在a中找任意長度為k的連續子串,然後問在b串中任意長度的子串首尾與a中這些長度為k的首尾相同的子串有多少個
題解:先把a串中的符合條件的找出來用vector 記錄一下,然後b串到著求一邊就行了,因為最多26個字母,用個sum[i][j]記錄i這個位置到最後j這個字元的數量即可
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int> v[28];
int vis[28][28];
int n,m,k;
char a[100010],b[100010];
int sum[100010][28];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
scanf("%s%s",a+1,b+1);
memset(vis,0,sizeof(vis));
for(int i=0;i<=25;i++)v[i].clear();
for(int i=1;i+k-1<=n;i++)
{
if(!vis[a[i]-'a'][a[i+k-1]-'a'])
{
v[a[i]-'a'].push_back(a[i+k-1]-'a');
vis[a[i]-'a'][a[i+k-1]-'a']=1;
}
}
for(int j=0;j<=26;j++)
sum[m+1][j]=0;
long long ans=0;
for(int i=m;i>=1;i--)
{
for(int j=0;j<=25;j++)
{
sum[i][j]=sum[i+1][j];
}
sum[i][b[i]-'a']++;
for(int j=0;j<v[b[i]-'a'].size();j++)
{
ans+=sum[i][v[b[i]-'a'][j]];
}
}
printf("%lld\n",ans);
}
return 0;
}