B. Strings of Power

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/318/problem/B

Description

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 106 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 %I64d specifier.

Sample Input

heavymetalisheavymetal

Sample Output

3

HINT

题意

一个字符串如果以heavy开头,metal结尾的话,就说明这是一个powerful句子,然后问你有多少个句子

题解:

对于metal,我们维护一个后缀和就好了

然后扫一遍就行啦……

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 4000001
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** string s;
//heavy
//metal
string s1="heavy";
string s2="metal";
int a[maxn];
int aa[maxn];
int b[maxn];
int bb[maxn];
int main()
{
cin>>s;
if(s.size()<)
{
cout<<"";
return ;
}
for(int i=;i<s.size()-;i++)
{
for(int j=;j<;j++)
{
if(s[i+j]!=s1[j])
break;
if(j==)
a[i]=;
}
for(int j=;j<;j++)
{
if(s[i+j]!=s2[j])
break;
if(j==)
b[i]=;
}
aa[i]=a[i];
bb[i]=b[i];
}
for(int i=;i<s.size();i++)
a[i]+=a[i-];
for(int i=s.size()-;i>=;i--)
b[i]+=b[i+]; ll ans=;
for(int i=;i<s.size();i++)
{
if(aa[i]==)
ans+=b[i+];
}
cout<<ans<<endl;
}

Codeforces Round #188 (Div. 2) B. Strings of Power 水题的更多相关文章

  1. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  2. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  3. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

  4. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

  5. Codeforces Round #368 (Div. 2) A. Brain's Photos 水题

    A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...

  6. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  7. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

  8. Codeforces Round #384 (Div. 2) A. Vladik and flights 水题

    A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

随机推荐

  1. Android FrameWork——相关内容

    http://blog.csdn.net/stonecao/article/category/781516

  2. 用js完成blog项目

    //前台调用 var $ = function (args) { return new Base(args); } //基础库 function Base(args) { //创建一个数组,来保存获取 ...

  3. JavaScript高级程序设计(第三版)第四章 变量,作用域和内存问题

    JavaScript变量可以用来保存两种类型的值:基本类型值和引用类型值.基本类型值和引用类型值具有以下特点: 基本类型值在内存中占据固定大小的空间,因此被保存在栈内存中: 从一个变量向另一个变量复制 ...

  4. Android学习笔记-Dialog详解

    1.对话框的使用 1.1AlertDialog的显示 简单对话框以及监听的设置:重点掌握三个按钮(也就是三上单词): PositiveButton(确认按钮);NeutralButton(忽略按钮) ...

  5. PHP获取curl的错误

    curl_error($ch) 用 curl_error 来获取 curl 的错误

  6. Maven 包命令

    1.必须选中项目,然后单击Run As,选择Maven build. 2.在配置窗体中的Goals栏填写clean package. 注意:Installed JREs中配置的JREs的位置必须是JD ...

  7. Hadoop 问题 & 解决

    1.将旧版本hadoop升级后,如从hadoop-1.1.2升级到hadoop-1.2.1,会发现使用start-all.sh命令,没有办法启动namenode,即jps,发现没有namenode 原 ...

  8. JQuery发送Put、Delete请求 - 摘自网络

    DELETE: $.ajax({ url: '/script.cgi', type: 'DELETE', success: function(result) { // Do something wit ...

  9. css font-face自定义字体

    font-face:自定义字体 1.找到一个字体文件 2.放入新建的的font的文件夹内: 3.在样式里面以下内容 注释:aa是字体名 <style> @font-face{     fo ...

  10. cocos2d-x在android中响应返回键编译报错的bug分析

    先看一段代码如何在Android中加入返回按键的响应 <span style="font-size:18px;">自己派生CCKeypadDelegate的子类,然后注 ...