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 @   GadyPu  阅读(242)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示