• 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
    ", ans);
    45     }
    46     return 0;
    47 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    HYSBZ 1500 [NOI2005]维修数列 splay
    The 15th Zhejiang University Programming Contest
    工作小助手-v1.0正式上线,欢迎体验!!!
    登录窗体登录失败但是MainForm依然弹出无法结束的解决方法
    报错'cannot change visible in onshow or onhide'
    release模式发布软件的方法
    发布软件时因为窗体自动加载次序不对导致报错00000000
    修改类别 (类实现)两种方法
    从记事本导入记录
    快速粘贴
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4624066.html
Copyright © 2020-2023  润新知