hdu 4150 Powerful Incantation

本文介绍了一个计算咒语威力的方法,通过统计咒语中特定“魔法指数”的出现次数来衡量其力量。采用简单直接的字符串匹配算法实现,适用于模式串较短的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目连接

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

转载于:https://www.cnblogs.com/GadyPu/p/4624066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值