H. Beautiful Substrings(思维)

滴答滴答---题目链接 

H. Beautiful Substrings

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

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 k is 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

Copy

2
4 5 3
acbd
abcbd
3 3 1
kkd
dkd

output

Copy

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.

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int vis[101][101];
int num[10101];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        char a[100011];
        char b[100001];
        scanf("%s%s",a,b);
       long long  int ans=0;
        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        for(int i=0; i+k-1<n; i++)
            vis[a[i]-'a'][a[i+k-1]-'a']=1;
        for(int i=0; i<m; i++)
        {
            num[b[i]-'a']++;
            for(int j=0; j<26; j++)
            {
                if(vis[j][b[i]-'a']==0)
                    continue;
                ans+=num[j];
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/chen_zan_yu_/article/details/83961100
今日推荐