string of power

来源:互联网 发布:37轩辕剑四极进阶数据 编辑:程序博客网 时间:2024/06/05 09:02

 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".


~!;

我感觉这题的最大难点是看懂题目,一旦看懂了题目就知道这只是一个简单的字符串问题了。

我们应该先得到第一个heavy,在想后面一个个搜索的出有几个metal,由此count就加几。然后再考虑第一个后的heavy是否还有heavy,有则继续上面操作。

#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    char stu[100000]={"0"}, result[5]="0";       int i,j,k,l,m,n,len,count;    while(scanf("%s",stu)!=EOF)    {        len=strlen(stu);i=0;j=0;count=0;m=0;n=0;        for(i=0;i<len;)        {                      n=0;                              for(j=i;j<i+5;j++)                {                   result[n]=stu[j];                    n++;                            }            if(strcmp(result,"heavy")==0)        {             for(l=i+5;l<len;)             {                n=0;                                                    for(m=l;m<l+5;m++)                {                   result[n]=stu[m];                   n++;                             }                   if(strcmp(result,"metal")==0)               {                   count++;l=m;                                        }               else               l++;             }                    i=j;                          }         else        i++;               }          printf("%d\n",count);                            getchar();                               }    //system("pause");return 0;    }



原创粉丝点击