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 is the length of string a, m 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 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的子串的尾,看他的首满足条件吗
看代码注释
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int T,n,m,k;
static String a,b,a1,b1;
static boolean[][] book=new boolean[30][30];//标记首尾,即字符串a存在这样首尾的子字符串
static int[] num=new int[30];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
T=sc.nextInt();
while(T>0){
n=sc.nextInt();
m=sc.nextInt();
k=sc.nextInt();
a=sc.next();
b=sc.next();
long sum=0;
for(int i=0;i<30;i++)
Arrays.fill(book[i],false);
Arrays.fill(num,0);
for(int i=0;i<a.length()-k+1;i++)
book[a.charAt(i)-'a'][a.charAt(i+k-1)-'a']=true;//标记首尾,即字符串a存在这样首尾的子字符串
for(int i=0;i<b.length();i++){
num[b.charAt(i)-'a']++;//记录各种字母 首的个数
for(int j=0;j<26;j++){
if(book[j][b.charAt(i)-'a']==false) continue;//定尾 如果以这样始末子字符串的没在数组标记过,即不满足条件
sum+=num[j];//满足条件,我们就看看这个尾前边有多少个满足条件的首
}
}
System.out.println(sum);
T--;
}
}
}