Codeforces Round #188 (Div. 2) B. Strings of Power

來源:互聯網
上載者:User
B. Strings of Powertime limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 than106 characters.

Output

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

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams
or the %I64dspecifier.

Sample test(s)input
heavymetalisheavymetal
output
3
input
heavymetalismetal
output
2
input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
output
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".

/*題目還是滿水的 不過TML了一次 WA了一次 寫個blog  紀念一下 漲點教訓 */#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;__int64 n,ans;int h[1000005],m[1000005],cnt1,cnt2,cnth,cntm;char s[1000005];void solve(){int i,j,len;cnt1=cnt2=0;len=strlen(s);for(i=0;i<=len-5;i++)    // 找出哪些位置出現了“heavy"和"metal" 並將首位置記錄在各自數組中    {        if(s[i]=='h')        {            if(s[i+1]=='e'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='y')            {                h[++cnt1]=i;            }        }        if(s[i]=='m')        {            if(s[i+1]=='e'&&s[i+2]=='t'&&s[i+3]=='a'&&s[i+4]=='l')            {                m[++cnt2]=i;            }        }    }    h[cnt1+1]=100000000;     // 類似貪心 要在最後加一個哨兵     m[cnt2+1]=100000005;    ans=0;    cnth=cntm=1;    while(cnth<=cnt1||cntm<=cnt2) // 開始用的二階迴圈 導致TMl 其實一階完全可以搞定    {        if(h[cnth]<m[cntm])  // 若不加哨兵 則cnth=cnt1後會一直執行語句一        {            cnth++;        }        else          // 同理m[]也要加哨兵  開始m[]沒加WA了一次        {            ans+=cnth-1;            cntm++;        }    }}int main(){int i,j,t;while(gets(s)!=NULL){solve();printf("%I64d\n",ans);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.