• 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;
    }
    


  • 相关阅读:
    Lc617_合并二叉树
    Lc257_二叉树的所有路径
    Lc222_完全二叉树的节点个数
    记github下载上传遇到的各种问题
    Lc101_对称二叉树
    Lc222_翻转二叉树
    二叉树的dfs 与 bfs (递归遍历)
    全球最火的程序员学习路线!没有之一!3天就在Github收获了接近1w点赞
    大二逃课总结的1.2w字的计算机网络知识!扫盲!
    「IDEA插件精选」安利一个IDEA骚操作:一键生成方法的序列图
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3178027.html
Copyright © 2020-2023  润新知