hdu 4150 Powerful Incantation

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4150

Powerful Incantation

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

3
aaaa aa
bsdjfassdiifo sd
papapapapapapap ap

Sample Output

2
2
7

由于模式串很短所以直接暴力即可。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 using std::cin;
 9 using std::cout;
10 using std::endl;
11 using std::find;
12 using std::sort;
13 using std::map;
14 using std::pair;
15 using std::vector;
16 using std::multimap;
17 #define pb(e) push_back(e)
18 #define sz(c) (int)(c).size()
19 #define mp(a, b) make_pair(a, b)
20 #define all(c) (c).begin(), (c).end()
21 #define iter(c) decltype((c).begin())
22 #define cls(arr,val) memset(arr,val,sizeof(arr))
23 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
24 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
26 const int N = 1000011;
27 typedef unsigned long long ull;
28 char text[N], pat[10];
29 int main() {
30 #ifdef LOCAL
31     freopen("in.txt", "r", stdin);
32     freopen("out.txt", "w+", stdout);
33 #endif
34     int t;
35     scanf("%d", &t);
36     while (t--) {
37         scanf("%s %s", text, pat);
38         char *p = text;
39         int ans = 0, len = strlen(pat);
40         while (p = strstr(p, pat)) {
41             ans++;
42             p += len;
43         }
44         printf("%d\n", ans);
45     }
46     return 0;
47 }
View Code
posted @ 2015-07-06 12:26  GadyPu  阅读(242)  评论(0编辑  收藏  举报