zoukankan      html  css  js  c++  java
  • Strings of Power

    B. Strings of Power

    Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.

    Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.

    For simplicity, let us assume that Volodya's text can be represented as a single string.

    Input

    Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 1e6 characters.

    Output

    Print exactly one number — the number of powerful substrings of the given string.

    Sample test(s)

    input

    heavymetalisheavymetal

    heavymetalismetal

    trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou

    output

    3

    2

    3

    Note

    In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.

    In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".

    继续水,不解释,贴代码;

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    char s[2000000];
    int main()
    {
        while(scanf("%s",s)!=EOF)
        {
            int length=strlen(s);
            long long a=0,b=0;
            for(int i=0;i<length;i++)
            {
                if(s[i]=='h'&&s[i+1]=='e'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='y')
                {
                    i+=4;
                    a++;
                }
                if(s[i]=='m'&&s[i+1]=='e'&&s[i+2]=='t'&&s[i+3]=='a'&&s[i+4]=='l')
                {
                    i+=4;
                    b+=a;
                }
            }
            printf("%d
    ",b);
        }
        return 0;
    }
    


  • 相关阅读:
    对象实例化内存布局与访问定位
    方法区

    本地方法栈
    本地方法接口
    虚拟机栈
    程序计数器
    运行时数据区概述及线程
    自学》2.网页弹窗计算商品价格
    自学》1.用网站发邮件
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3178027.html
Copyright © 2011-2022 走看看