Powerful Incantation
HDU - 4150
Some dangerous Witches invaded the garden city! As a righteous magician, james0zan want to find the most powerful incantation so he can beat those dangerous witches easily.
After consulted one hundred and eight thousand Magic Books, james0zan find a way to calculate the power of an incantation. There is a very short incantation called “magic index” which contains the magic power, and each incantation’s power can be calculated by the times the “magic index” appearance in the incantation. Notice that if two “magic index” overlapped, the power only should be calculated once. And we just want the incantation more powerful. That is to say, if the “magic index” is “aa”, the power of incantation “aaaa” is 2(“aa”+”aa”), not 1(“a”+”aa”+”a”) or 3.
After consulted one hundred and eight thousand Magic Books, james0zan find a way to calculate the power of an incantation. There is a very short incantation called “magic index” which contains the magic power, and each incantation’s power can be calculated by the times the “magic index” appearance in the incantation. Notice that if two “magic index” overlapped, the power only should be calculated once. And we just want the incantation more powerful. That is to say, if the “magic index” is “aa”, the power of incantation “aaaa” is 2(“aa”+”aa”), not 1(“a”+”aa”+”a”) or 3.
Input
The first line is an integer t (t<=30), the number of test cases. Each of the next t lines contains two strings, represent the incantation (length<=10^6) and the “magic index” (length<=5). Every char in the incantation and the magic index is lowercase.
Output
For each test case you should output the power of the incantation.
Sample Input
3
aaaa aa
bsdjfassdiifo sd
papapapapapapap ap
Sample Output
2
2
7
可以说是非常水了。读个字符串,然后再读要查找的串,选0到strlen-1为起始点,再在内层循环逐个比较字符,这里如果有一个字符不一样就break,记有几个字符一样,如果和查找串的长度一样把外层循环的计数器放在当前找到的串的最后一位(因为不能重叠)。一遍过没什么好说。
#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d\n",&t);
while(t--)
{
char ss[1000002],sss[6];
scanf("%s %s",ss,sss);
//printf("%c %c\n",ss[3],sss[1]);
int i,summ=0;
for(i=0;i<=strlen(ss)-1;i++)
{
int j,sum=0;
for(j=0;j<=strlen(sss)-1;j++)
{
if(ss[i+j]==sss[j])sum++;
else break;
}
if(sum==strlen(sss))
{
i=i+j-1;
summ++;
}
}
printf("%d\n",summ);
}
return 0;
}