Powerful Incantation(HDU 4150)

来源:互联网 发布:安卓手机淘宝网 编辑:程序博客网 时间:2024/06/08 06:27

Powerful Incantation

Time Limit: 2000 MSMemory Limit: 32768 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

Description

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.

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

3aaaa aabsdjfassdiifo sdpapapapapapapap ap

Sample Output

227
题意:输入一个字符串a,一个字符串b,b是a的字串,也就是字符串的匹配,必须是连续的,不能拆开。。。
代码:
#include<stdio.h>
#include<string.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char a[2000000],b[20];        //这里数组要开的足够大
        scanf("%s%s",a,b);
        int sum=0;                           //记录个数
        int la=strlen(a);
        int lb=strlen(b);
        for(int i=0;i<la;i++)
        {
            int t=0;                        //标记变量,之所以要在这里定义是因为每次a的数组要循环
            for(int j=0;j<lb;j++)
            {
                if(a[i+j]!=b[j])       //这里检测是否是子串
                {
                    t=1;
                    break;
                }
            }
            if(t==0)
            {
                sum++;
                i=i+lb-1;            //这里这个减一极其极其重要,因为如果不加一就会跳过一个字符,因为后边有i++
            }
        }
        printf("%d\n",sum);
    }
}




0 0
原创粉丝点击