GYM101972H

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的子串的尾,看他的首满足条件吗

看代码注释

 

 

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

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值