首页 > ACM题库 > HDU-杭电 > HDU 4150-Powerful Incantation[解题报告]HOJ
2015
04-16

HDU 4150-Powerful Incantation[解题报告]HOJ

Powerful Incantation

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

3
aaaa aa
bsdjfassdiifo sd
papapapapapapap ap

样例输出:

2
2
7

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

char a[1111111], b[11];

int main()
{
    int t, i, j, ans, lena, lenb;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s%s", a, b);
        lena = strlen(a);
        lenb = strlen(b);
        ans = 0;
        for(i = 0; i < lena; i++)
        {
            for(j = 0; j < lenb; j++)
                if(a[i + j] != b[j])
                    break;
            if(j == lenb)
                ans++, i += j - 1;
        }
        printf("%d\n", ans);
    }
}

  1. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。