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; }
- string of power
- Power of two,power of three,power of four
- [LeetCode] Power of Three | Power of Two
- FOJ1011 Power String
- POJ 2406 Power String
- power string kmp算法
- POJ2406 Power String
- POJ 2406 - Power String
- The power of Reading
- The power of algorithm
- Power of Cryptography POJ2109
- JOJ1728:Power of Cryptography
- poj21029 Power of Cryptography
- POJ2109 Power of Cryptography
- 113 - Power of Cryptography
- UVa Power of Cryptography
- 113 - Power of Cryptography
- F - Power of Cryptography
- 工作以后~流水账版(不定期更新)
- 运算符重载时到底发生了什么?【DEBUG】
- 黑马程序员——线程
- python join 和 split的常用使用方法
- STL源码剖析之一:概论
- string of power
- [选拔赛] Even Odds
- Linux新手入门:通过chmod改变文件权限
- 19 Tools to Test your Site for Mobile Devices
- [选拔赛]Strings of Power
- Linux内核模块基础
- gedit打开txt乱码
- ALV如何取数实例
- 在easyui-combobox多选模式下加入选择所有选项