//代码来自某博客,不记得了,博主可以留言,我即刻加上,这里只作为一个例子
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
char s[N * 10], p[N];
int nextval[N];
int lens, lenp;
//得到匹配串p的next数组,注意next数组名不能起为next[]和系统保留字冲突,可以是nxt[]
void getnext()
{
//不断在前后移动的j指针是匹配串的,一直从前往后的指针i是主串的,因此kmp的复杂度也是主串的长度O(n)
int i = 0, j = -1;
nextval[0] = -1;
while(i < lenp)
{
if(j == -1 || p[i] == p[j])
nextval[++i] = ++j;
else
j = nextval[j];
}
}
int KMP()
{
int i = 0, j = 0, count = 0;
//这里初始化注意不是一个是-1一个是0了,因为都是从第0个开始比较,否则岂不是主串第一个字母根本没去匹配,直接用第二个字母和匹配串第一个字母去匹配了是不对的
while(i <lens && j < lenp)
{
if(s[i] == p[j] || j == -1)
++i, ++j;
else
j = nextval[j];
if(j == lenp)//匹配上了一个,若要返回匹配的位置则返回return j-i;
{
count++;
j = nextval[j];
}
}
return count;
}
int main()
{
int ncase;
int len;
int ans;
scanf("%d", &ncase);
while(ncase--)
{
scanf("%s%s", p, s);//s是主串,p是匹配串
lens = strlen(s);
lenp = strlen(p);
getnext();
ans = KMP();
printf("%d\n", ans);
}
return 0;
}