Strings of Power

来源:互联网 发布:阿里云服务器部署项目 编辑:程序博客网 时间:2024/06/05 15:51

Strings of Power

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

Volodya calls a string powerful if it startswith "heavy" and ends with "metal". Finding all powerfulsubstrings (by substring Volodya means a subsequence of consecutive charactersin a string) in a given text makes our hero especially joyful. Recently he feltan enormous fit of energy while reading a certain text. So Volodya decided tocount all powerful substrings in this text and brag about it all day long. Helphim in this difficult task. Two substrings are considered different if theyappear at the different positions in the text.

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

 

Input

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

 

Output

Print exactly one number — thenumber 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 certainlypowerful.

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

分析:

此题的意思就是输入的一句话中有多少个从heavy开头到metal结束的句子。

主要就是用string数组来放句子。

代码:

#include<iostream>  
#include<string.h>   
using namespace std;  
  
int main()  
{  char s[1000000];
    while(cin>>s)  
    {  
        int length=strlen(s);  
       int x=0,y=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')  
            {   
                x++;  
            }  
            if(s[i]=='m'&&s[i+1]=='e'&&s[i+2]=='t'&&s[i+3]=='a'&&s[i+4]=='l')  
            {   
                y+=x;  
            }  
        }  
        cout<<y<<endl;  
    }  
    return 0;  

原创粉丝点击