zoukankan      html  css  js  c++  java
  • 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/ 转载请说明
  • 相关阅读:
    转载两篇文章之二(从程序员到CTO所要培养的六种能力)作者:阿蒙 原文:http://blog.csdn.net/harrymeng/archive/2007/02/07/1503931.aspx
    关于解决eclipse中的插件依赖
    GMF中控制Figure的大小和位置
    Sql server2005中如何格式化时间日期
    JavaScript创建对象的几种方式
    FCK编辑器焦点问题
    对引用类型的一点理解
    利用XML FOR PATH 合并分组信息
    [译]Silverlight中TreeView增删改查和拖放
    KeyedCollection<TKey, TItem>与IDictionary<TKey, TValue>的区别
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4624066.html
Copyright © 2011-2022 走看看