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 nm, and k (1 ≤ n, m ≤ 105, 1 ≤ k ≤ n), in which n is the length of string am is the length of string b, and kis the described variable in the statement.

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 slsl + 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;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/85030324