Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
318_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 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. Examples 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".
2
8
ans = 0 s = raw_input() heavy = 0 i = 0 if not len(s) < 5: while i < len(s) - 4: if s[i] == 'h' and s[i + 1] == 'e' and s[i + 2] == 'a' and s[i + 3] == 'v' and s[i + 4] == 'y': heavy += 1 i += 5 elif s[i] == 'm' and s[i + 1] == 'e' and s[i + 2] == 't' and s[i + 3] == 'a' and s[i + 4] == 'l': ans += heavy i += 5 else: i += 1 print ans #a = raw_input()
PYTHON
318_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 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. Examples 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".
2
8
//"We have two lives, and the second begins when we realize we only have one." β€” Confucius import java.util.*; import java.io.*; public class A { public static void main(String[] args) { FastScanner fs = new FastScanner(); String str= fs.next(); String prefix="heavy",suffix="metal"; long ans=0;long pre=0; for(int i=0;i<str.length();i++) { if(i<str.length()-4 && str.substring(i,i+5).equals(prefix))pre++; if(i<str.length()-4 && str.substring(i,i+5).equals(suffix))ans+=pre; } System.out.println(ans); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
JAVA
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long t = s.size(), c = 0, cnt = 0; for (int i = 0; i < t - 4; ++i) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') c++; if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') cnt += c; } cout << cnt << '\n'; }
CPP
318_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 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. Examples 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".
2
8
#!/usr/bin print sum(-~c * g.count('metal') for c, g in enumerate(raw_input().split('heavy')[1:]))
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; long long int modular_pow(long long int base, long long int exponent) { long long int result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % 1000003; exponent = exponent >> 1; base = (base * base) % 1000003; } return result; } long long int gcd(long long int a, long long int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int main() { char s[1000005]; scanf("%s", s); char ss[6]; int n, m, i, k, l, j, c1 = 0, f; long long int ans = 0; n = strlen(s); for (i = 0; i < n - 4; i++) { strncpy(ss, s + i, 5); ss[5] = '\0'; if (strcmp(ss, "heavy") == 0) c1++; if (strcmp(ss, "metal") == 0) ans += c1; } printf("%I64d\n", ans); return 0; }
CPP
318_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 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. Examples 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".
2
8
L = input().split('heavy') ans = 0 for i in range(len(L)): ans += i*L[i].count('metal') print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
from bisect import bisect_left s, starts, start, k = input(), [], 0, 0 while True: pos = s.find('heavy', start) start = pos + 1 if pos < 0: break starts.append(pos) while True: pos = s.find('metal', start) if pos < 0: print(k) break start, k = pos + 1, k + bisect_left(starts, pos)
PYTHON3
318_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 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. Examples 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".
2
8
# -*- coding:utf-8 -*- import sys def some_func(): """ """ num = 0 count = 0 s_flag = 0 e_flag = 0 while True: data = sys.stdin.read(1) if data == "\n": break if data == "h": s_flag = 1 e_flag = 0 elif data == "e" and s_flag ==1: s_flag = 2 e_flag = 0 elif data == "a" and s_flag ==2: s_flag = 3 e_flag = 0 elif data == "v" and s_flag ==3: s_flag = 4 e_flag = 0 elif data == "y" and s_flag ==4: s_flag = 0 e_flag = 0 count+=1 elif data =="m": s_flag = 0 e_flag = 1 elif data == "e" and e_flag ==1: s_flag = 0 e_flag = 2 elif data == "t" and e_flag ==2: s_flag = 0 e_flag = 3 elif data == "a" and e_flag ==3: s_flag = 0 e_flag = 4 elif data == "l" and e_flag ==4: s_flag = 0 e_flag = 0 num +=count else: s_flag = 0 e_flag = 0 print num if __name__ == '__main__': some_func()
PYTHON
318_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 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. Examples 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".
2
8
s = input() a = [] for i in range(len(s)): c = s[i:i+5] if c == 'heavy': a.append(0) elif c == 'metal': a.append(1) k = 0 ans = 0 for i in a: if i == 0: k += 1 else: ans += k print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.TreeMap; public class StringPower { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String word = in.readLine(); TreeMap<Integer, Integer> beginMap = getIndexes(word, "heavy"); TreeMap<Integer, Integer> endMap = getIndexes(word, "metal"); if(beginMap.size()==0 || endMap.size()==0) System.out.println(0); else System.out.println(calculateTrees(beginMap, endMap)); } private static long calculateTrees(TreeMap<Integer, Integer> beginMap, TreeMap<Integer, Integer> endMap) { long count = 0; int beginSize = beginMap.size(); int endSize = endMap.size(); Iterator<Integer> it = beginMap.keySet().iterator(); while(it.hasNext()){ int key = it.next(); int key2; try { key2 = endMap.ceilingKey(key); } catch (NullPointerException e) { continue; } count+= endSize - endMap.get(key2); } return count; } private static TreeMap<Integer, Integer> getIndexes(String word, String token) { TreeMap<Integer, Integer> tree = new TreeMap<Integer, Integer>(); int key = 0; for(int i = 0; i<word.length(); ){ int index = word.indexOf(token, i); if(index==-1) break; tree.put(index, key); key++; i=index+token.length(); } return tree; } }
JAVA
318_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 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. Examples 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".
2
8
c = 0 for x,z in enumerate(input().split("heavy")): c += x*z.count("metal") print(c)
PYTHON3
318_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 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. Examples 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".
2
8
R=raw_input() m=t=0 for i in range(5,len(R)+1): t+=R[i-5:i]=="heavy" m+=t*(R[i-5:i]=="metal") print m
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; long long c = 0; long long q = 0; char a[str.size()]; long long b[str.size()]; for (int k = 0; k < str.size(); k++) { a[k] = str.at(k); } if (str.size() < 5) { cout << "0" << endl; return 0; } b[str.size() - 5] = 0; b[str.size() - 4] = 0; b[str.size() - 3] = 0; b[str.size() - 2] = 0; b[str.size() - 1] = 0; cout << endl; for (int k = str.size() - 5; k >= 0; k--) { if (a[k] == 'm' && a[k + 1] == 'e' && a[k + 2] == 't' && a[k + 3] == 'a' && a[k + 4] == 'l') { c++; } b[k] = c; } for (int k = 0; k < str.size() - 6; k++) { if (a[k] == 'h' && a[k + 1] == 'e' && a[k + 2] == 'a' && a[k + 3] == 'v' && a[k + 4] == 'y') { q += b[k]; } } cout << q << endl; }
CPP
318_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 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. Examples 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".
2
8
import sys from collections import defaultdict, Counter from itertools import permutations, combinations from math import sin, cos, asin, acos, tan, atan, pi sys.setrecursionlimit(10 ** 6) def pyes_no(condition, yes = "YES", no = "NO", none = "-1") : if condition == None: print (none) elif condition : print (yes) else : print (no) def plist(a, s = ' ') : print (s.join(map(str, a))) def rint() : return int(sys.stdin.readline()) def rstr() : return sys.stdin.readline().strip() def rints() : return map(int, sys.stdin.readline().split()) def rfield(n, m = None) : if m == None : m = n field = [] for i in xrange(n) : chars = sys.stdin.readline().strip() assert(len(chars) == m) field.append(chars) return field def pfield(field, separator = '') : print ('\n'.join(map(lambda x: separator.join(x), field))) def check_field_equal(field, i, j, value) : if i >= 0 and i < len(field) and j >= 0 and j < len(field[i]) : return value == field[i][j] return None def digits(x, p) : digits = [] while x > 0 : digits.append(x % p) x //= p return digits[::-1] def undigits(x, p) : value = 0 for d in x : value *= p value += d return value def modpower(a, n, mod) : r = a ** (n % 2) if n > 1 : r *= modpower(a, n // 2, mod) ** 2 return r % mod def gcd(a, b) : if a > b : a, b = b, a while a > 0 : a, b = b % a, a return b def vector_distance(a, b) : diff = vector_diff(a, b) return scalar_product(diff, diff) ** 0.5 def vector_inverse(v) : r = [-x for x in v] return tuple(r) def vector_diff(a, b) : return vector_sum(a, vector_inverse(b)) def vector_sum(a, b) : r = [c1 + c2 for c1, c2 in zip(a, b)] return tuple(r) def scalar_product(a, b) : r = 0 for c1, c2 in zip(a, b) : r += c1 * c2 return r def check_rectangle(points) : assert(len(points) == 4) A, B, C, D = points for A1, A2, A3, A4 in [ (A, B, C, D), (A, C, B, D), (A, B, D, C), (A, C, D, B), (A, D, B, C), (A, D, C, B), ] : sides = ( vector_diff(A1, A2), vector_diff(A2, A3), vector_diff(A3, A4), vector_diff(A4, A1), ) if all(scalar_product(s1, s2) == 0 for s1, s2 in zip(sides, sides[1:])) : return True return False def check_square(points) : if not check_rectangle(points) : return False A, B, C, D = points for A1, A2, A3, A4 in [ (A, B, C, D), (A, C, B, D), (A, B, D, C), (A, C, D, B), (A, D, B, C), (A, D, C, B), ] : side_lengths = [ (first[0] - next[0]) ** 2 + (first[1] - next[1]) ** 2 for first, next in zip([A1, A2, A3, A4], [A2, A3, A4, A1]) ] if len(set(side_lengths)) == 1 : return True return False def check_right(p) : # Check if there are same points for a, b in [ (p[0], p[1]), (p[0], p[2]), (p[1], p[2]), ] : if a[0] == b[0] and a[1] == b[1] : return False a, b, c = p a, b, c = vector_diff(a, b), vector_diff(b, c), vector_diff(c, a) return scalar_product(a, b) * scalar_product(a, c) * scalar_product(b, c) == 0 def modmatrixproduct(a, b, mod) : n, m1 = len(a), len(a[0]) m2, k = len(b), len(b[0]) assert(m1 == m2) m = m1 r = [[0] * k for i in range(n)] for i in range(n) : for j in range(k) : for l in range(m) : r[i][j] += a[i][l] * b[l][j] r[i][j] %= mod return r def modmatrixpower(a, n, mod) : magic = 2 for m in [2, 3, 5, 7] : if n % m == 0 : magic = m break r = None if n < magic : r = a n -= 1 else : s = modmatrixpower(a, n // magic, mod) r = s for i in range(magic - 1) : r = modmatrixproduct(r, s, mod) for i in range(n % magic) : r = modmatrixproduct(r, a, mod) return r heavy = 0 count = 0 s = rstr() n = len(s) i = 0 while i < n - 4 : if s[i:i + 5] == 'heavy' : heavy += 1 i += 5 elif s[i:i + 5] == 'metal' : count += heavy i += 5 else : i += 1 print count
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int inf = 2000000000; int arrh[1000005]; int arrm[1000005]; char strh[] = "heavy"; char strm[] = "metal"; int main() { char str[1000005]; scanf("%s", str); int l = strlen(str); int h = 0, m = 0; int pos = 0; long long ans = 0; while (1) { if (pos >= l) break; int pos1 = pos; if (str[pos] == 'h') { pos++; bool ok = 1; int posh = 1; while (1) { if (posh >= 5) break; if (str[pos] == strh[posh]) { pos++; posh++; } else { ok = 0; break; } } if (ok == 1) { arrh[h] = pos1; h++; pos = pos1 + 5; } else { pos = pos1 + 1; } } else { pos = pos1 + 1; } } pos = 0; while (1) { if (pos >= l) break; int pos1 = pos; if (str[pos] == 'm') { pos++; bool ok = 1; int posm = 1; while (1) { if (posm >= 5) break; if (str[pos] == strm[posm]) { pos++; posm++; } else { ok = 0; break; } } if (ok == 1) { arrm[m] = pos1; m++; pos = pos1 + 5; } else { pos = pos1 + 1; } } else { pos = pos1 + 1; } } int i, j; for (i = 0; i < h; i++) { int lo = 0; int hi = m; int mid; while (lo < hi) { mid = lo + (hi - lo) / 2; if (arrm[mid] > arrh[i]) { hi = mid; } else lo = mid + 1; } int midans = m - lo; ans += midans; } printf("%I64d\n", ans); return 0; }
CPP
318_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 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. Examples 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".
2
8
import java.util.*; import java.io.*; public class A { public static void main(String[] args) throws IOException { S = rd.readLine(); int[] app = new int[1000001]; for(int i=0; i<S.length()-4; i++){ if(S.substring(i, i+5).equals("heavy")) app[i] = 1; } for(int i=0; i<S.length(); i++) app[i] += (i==0? 0: app[i-1]); long ans = 0; long cur = app[Math.max(0, S.length()-4)]; for(int i=S.length()-5; i>=0; i--){ if(S.substring(i, i+5).equals("metal")){ ans += cur; } if(i!=0 && app[i]!=app[i-1]) cur--; } System.out.println(ans); } static int appIn(int x, int y, int[] app){ return x>0? app[y]-app[x-1]: app[y]; } static String S; static StringTokenizer st; static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw = new PrintWriter(System.out); }
JAVA
318_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 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. Examples 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".
2
8
input_str = input() if input_str.find('heavy') == -1: print(0) else: a = input_str.index('heavy')+5 b = 1 result = 0 while a < len(input_str): if input_str[a:(a+5)] == 'heavy': b+=1 a+=4 elif input_str[a:(a+5)] == 'metal': result+=b a+=4 else: a+=1 print(result)
PYTHON3
318_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 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. Examples 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".
2
8
str = input() h=0 m=0 for i in range(len(str)-4): if str[i:i+5] =="heavy": h+=1 if str[i:i+5] == "metal": m+=h print(m)
PYTHON3
318_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 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. Examples 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".
2
8
s = input().strip() H = s.split("heavy") co,k = 0,0 for i in H: co += i.count("metal")*k k += 1 print(co)
PYTHON3
318_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 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. Examples 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".
2
8
hc = 0 total = 0 while 1: try: line = raw_input() length = len(line) pos = 0 while pos <= length - 5: stri = line[pos:pos+5] pos += 1 if stri == "heavy": hc += 1 pos += 4 elif stri == "metal": total += hc pos += 4 except: break print total
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; bool is_heavy(string& s, int ind) { return s[ind + 3] == 'v' && s[ind] == 'h' && s[ind + 1] == 'e' && s[ind + 2] == 'a' && s[ind + 4] == 'y'; } bool is_metal(string& s, int ind) { return s[ind + 2] == 't' && s[ind] == 'm' && s[ind + 1] == 'e' && s[ind + 3] == 'a' && s[ind + 4] == 'l'; } int main() { string s; s.reserve(1000000); int ch; while ((ch = getchar()) != '\n' && ch != EOF) { s.push_back(ch); } vector<int> h, m; h.reserve(s.size() / 5); m.reserve(s.size() / 5); for (int i = 0; i < (int)s.size() - 4; ++i) { if (is_heavy(s, i)) { h.push_back(i); i += 4; } else if (is_metal(s, i)) { m.push_back(i); i += 4; } } long long res = 0; int hi = 0, mi = 0; for (int i = 0; i < h.size() && mi < m.size(); ++i) { while (mi < m.size() && m[mi] <= h[i]) { ++mi; } res += m.size() - mi; } cout << res << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
import re s = raw_input() heavy = [] metal = [] for m in re.finditer('heavy', s): heavy.append(m.start()) for m in re.finditer('metal', s): metal.append(m.start()) nh = len(heavy) nm = len(metal) ih = im = 0 total = 0 while ih < nh and im < nm: if metal[im] > heavy[ih]: total += nm - im ih += 1 else: im += 1 print total
PYTHON
318_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 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. Examples 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".
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[]args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int l=s.length()-4; int c=0; long counter=0; int j=5; for(int i=0;i<l;i++) { if(s.substring(i,j).equals("heavy")) c++; if(s.substring(i,j).equals("metal")) counter+=c; j++; } System.out.println(counter); } }
JAVA
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; string s; int r[1000013] = {0}; int main() { cin >> s; int l = s.size(); int count = 0; for (int i = l - 1; i >= 0; i--) { if (s[i] == 'm' && i >= 4) { string temp(s.begin() + i, s.begin() + i + 5); if (temp == "metal") { count++; } } r[i] = count; } long long ans = 0; for (int i = 0; i < l; i++) { if (i <= (l - 5)) { string temp(s.begin() + i, s.begin() + i + 5); if (temp == "heavy") ans = ans + r[i]; } } cout << ans; return 0; }
CPP
318_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 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. Examples 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".
2
8
s=raw_input() h,v,a,b=0,0,'zzzzz','zzzzz' for c in s: a,b=a[1:]+c,b[1:]+c if a=='heavy':h+=1 elif b=='metal':v+=h print v
PYTHON
318_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 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. Examples 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".
2
8
__author__ = 'Azad' import re import bisect s = raw_input() # q = [m.start() for m in re.finditer('heavy', s)] #print [m.start() for m in re.finditer('metal', s)] e = [m.start() for m in re.finditer('metal', s)] i = bisect.bisect_left(e, 0) y = len(e) print sum([ y-bisect.bisect_left(e, x) for x in q]) #print i
PYTHON
318_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 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. Examples 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".
2
8
import io,os input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import deque as que, defaultdict as vector from bisect import bisect as bsearch from heapq import* inin = lambda: int(input()) inar = lambda: list(map(int,input().split())) inst= lambda: input().decode().rstrip('\n\r') INF=float('inf') '''from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc''' _T_=1 #inin() for _t_ in range(_T_): s=inst() hv=0 ans=0 for i in range(len(s)-4): if s[i:i+5]=='heavy': hv+=1 if s[i:i+5]=='metal': ans+=hv print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); Queue<Integer> h = new LinkedList<Integer>(); Queue<Integer> m = new LinkedList<Integer>(); int index = -1; while((index = str.indexOf("heavy", index+1)) >= 0) { h.add(index); } index = -1; while((index = str.indexOf("metal", index+1)) >= 0) { m.add(index); } long count = 0; while(h.size()>0 && m.size()>0) { while(m.size()>0 && m.peek().intValue()<=h.peek().intValue()) { m.remove(); } count += m.size(); h.remove(); } System.out.println(count); } }
JAVA
318_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 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. Examples 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".
2
8
import java.util.*; public class B { static final String HEAVY = "heavy"; static final String METAL = "metal"; static long [] find(String text, String pattern){ final long [] res = new long[text.length()]; for (int i = text.indexOf(pattern); i >= 0; i = text.indexOf(pattern, i + pattern.length())){ res[i] = 1; } return res; } public static void main(String [] args){ Scanner s = new Scanner(System.in); final String text = s.nextLine(); final int n = text.length(); final long [] b = find(text, HEAVY); final long [] e = find(text, METAL); for (int i = n - 2; i >= 0; --i){ e[i] += e[i + 1]; } long res = 0; for (int i = 0; i < n - HEAVY.length(); ++i){ res += b[i]*e[i + HEAVY.length()]; } System.out.println(res); s.close(); } }
JAVA
318_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 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. Examples 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".
2
8
from bisect import bisect as bs s = input() posHeavy = [] posMetal = [] for i in range(len(s)-4): if s[i:i+5]=='heavy': posHeavy.append(i+4) for i in range(len(s)-4): if s[i:i+5]=='metal': posMetal.append(i) res = 0 for i in posHeavy: res+=len(posMetal)-bs(posMetal,i) print(res)
PYTHON3
318_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 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. Examples 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".
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Metal { public static void main(String[] args) throws IOException{ BufferedReader br; br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int i; int l = s.length(); long[] h = new long[l+1]; h[l] = 0; for (i=l-1; i>=0; i--){ h[i] = s.charAt(i) + h[i+1]*127; } long heavy=0, metal=0; for (i=4; i>=0; i--){ heavy = "heavy".charAt(i) + heavy*127; metal = "metal".charAt(i) + metal*127; } long numH = 0; long resp = 0; for (i=0; i<=l-5; i++){ long aux = (h[i]-h[i+5]*(long)Math.pow(127,5)); if (aux == heavy) numH++; else if (aux == metal) resp += numH; } System.out.println(resp); } }
JAVA
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; long long a[1234567]; int main() { long long k = 0, l = 0; string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') l++; a[i] = l; } for (int i = 0; i < s.length(); i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') k += (a[s.length() - 1] - a[i]), i += 4; } cout << k << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
line = raw_input() total_count = 0 try: lines = line.split('heavy') for i, line in enumerate(lines): total_count += line.count('metal') * i except Exception: pass print total_count
PYTHON
318_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 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. Examples 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".
2
8
import static java.lang.System.*; import static java.lang.Math.*; import java.util.*; public class B318{ public static Scanner sc = new Scanner(in); //public static Random sc=new Random(); public static int upper_bound(Integer[] a,int val) { int f=0,l=a.length; int len=l-f; while(len>0){ int half=len>>1; int mid=f+half; if(val<a[mid]){ len=half; }else{ f=mid; ++f; len=len-half-1; } } return f; } public void run(){ String str=sc.next(); List<Integer> hind=new ArrayList<Integer>(); List<Integer> mind=new ArrayList<Integer>(); int s=0; while(s<str.length()){ int v=str.indexOf("heavy",s); if(v==-1)break; hind.add(v); s=v+1; } s=0; while(s<str.length()){ int v=str.indexOf("metal",s); if(v==-1)break; mind.add(v); s=v+1; } Integer[] hs=hind.toArray(new Integer[0]); Integer[] ms=mind.toArray(new Integer[0]); long res=0; for(int i=0;i<hs.length;i++){ int v=upper_bound(ms,hs[i]); res+=ms.length-v; } ln(res); } public static void main(String[] _) { new B318().run(); } public int[] nextIntArray(int n){ int[] res=new int[n]; for(int i=0;i<n;i++){ res[i]=sc.nextInt(); } return res; } public static void pr(Object o) { out.print(o); } public static void ln(Object o) { out.println(o); } public static void ln() { out.println(); } }
JAVA
318_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 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. Examples 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".
2
8
def solve(s): c, r = 0, 0 for i in range(5, len(s) + 1): if s[i - 5:i] == 'heavy': c += 1 elif s[i - 5:i] == 'metal': r += c return r print(solve(input()))
PYTHON3
318_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 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. Examples 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".
2
8
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { public static void main(String [] args ) { try{ String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedOutputStream bos = new BufferedOutputStream(System.out); String eol = System.getProperty("line.separator"); byte [] eolb = eol.getBytes(); byte[] spaceb= " ".getBytes(); str = br.readLine(); int length = str.length(); ArrayList<Integer> listOne = new ArrayList<Integer>(); ArrayList<Integer> listTwo = new ArrayList<Integer>(); for(int i = 0 ; i <= length-5 ; i++) { String st = str.substring(i,i+5); if(st.equals("heavy")) { listOne.add(i); } if(st.equals("metal")) { listTwo.add(i); } } int iter1 = 0; int iter2 = 0; long ans = 0; while((iter1<listOne.size())&&(iter2<listTwo.size())) { if(listOne.get(iter1)<listTwo.get(iter2)) { ans += (listTwo.size()-iter2); iter1++; } else { iter2++; } } bos.write(new Long(ans).toString().getBytes()); bos.write(eolb); bos.flush(); } catch(IOException ioe) { ioe.printStackTrace(); } } }
JAVA
318_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 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. Examples 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".
2
8
ans = count = 0 for s in raw_input().split("heavy"): ans += s.count("metal") * count count += 1 print ans
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int main() { char in[1000009]; cin >> in; int len = strlen(in); int pre = 0; long long int ans = 0; for (int i = 0; i <= len - 5; i++) { if (in[i] == 'h' && in[i + 1] == 'e' && in[i + 2] == 'a' && in[i + 3] == 'v' && in[i + 4] == 'y') { pre++; } if (in[i] == 'm' && in[i + 1] == 'e' && in[i + 2] == 't' && in[i + 3] == 'a' && in[i + 4] == 'l') { ans += pre; } } cout << ans << "\n"; return 0; }
CPP
318_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 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. Examples 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".
2
8
w=map(str,raw_input().split('heavy')) ans=chk=be=0 ic=[] for i in range(1,len(w)): if 'metal' in w[i]: ic.append(w[i].count('metal')) else: ic.append(0) ans=chk=sum(ic) for i in range(1,len(ic)+1): chk-=ic[i-1] ans+=chk print ans
PYTHON
318_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 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. Examples 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".
2
8
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); int test_cases = 1; Solver s = new Solver(); for (int i = 1; i <= test_cases; i++) { s.solve(i, in, out); } out.close(); } } class Solver { void solve(int test_number, InputReader in, PrintWriter out) throws IOException { String s = in.next(); long h = 0, m = 0; for (int i = 0; i <= s.length(); i++) { if (i > 4) { if (check(s.substring(i - 5, i), 1)) { h++; } if (check(s.substring(i - 5, i), 2)) { m += h; } } } out.println(m); } static boolean check(String s, int c) { if (c == 1 && s.equals("heavy")) return true; if (c == 2 && s.equals("metal")) return true; return false; } } class InputReader { BufferedReader br; StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, len, r, res, flag; char s[1000005], h[10] = "heavy", m[10] = "metal"; cin >> s; len = strlen(s); r = 0; res = 0; for (i = 0; i < len; i++) { flag = 0; if (s[i] == 'h') { for (j = 0; j < 5; j++) { if (s[i + j] != h[j]) flag = 1; } if (!flag) r++; } if (s[i] == 'm') { for (j = 0; j < 5; j++) { if (s[i + j] != m[j]) flag = 1; } if (!flag) res += r; } } cout << res << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
s=input().split('heavy') start=0 rs=0 for i in range(1,len(s)): rs=rs+s[i].count('metal')*i print(rs)
PYTHON3
318_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 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. Examples 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".
2
8
import java.util.Scanner; /** * * @author claudio */ public class Main{ public static void main(String [] args) { Scanner sc=new Scanner(System.in); String palabra=sc.nextLine(); long heavy =0; long total=0; for (int i = 0; i < palabra.length()-4; i++) { if(palabra.substring(i,i+5).equals("heavy")) { heavy++; } else { if(palabra.substring(i, i+5).equals("metal")) { total=total+heavy; } } } System.out.println(total); } }
JAVA
318_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 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. Examples 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".
2
8
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.*; import java.util.*; /** * * @author N-AssassiN */ public class B { private static BufferedReader reader; private static PrintWriter out; private static StringTokenizer tokenizer; //private final static String filename = "filename"; private static void init(InputStream input, OutputStream output) throws IOException { reader = new BufferedReader(new InputStreamReader(input)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output))); //reader = new BufferedReader(new FileReader(filename + ".in")); //out = new BufferedWriter(new FileWriter(filename + ".out")); tokenizer = new StringTokenizer(""); } private static String nextLine() throws IOException { return reader.readLine(); } private static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(nextLine()); return tokenizer.nextToken(); } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { init(System.in, System.out); char[] s = nextLine().toCharArray(); //long startTime = System.currentTimeMillis(); long ans = 0; long temp = 0; if (s.length >= 10) { int limit = s.length - 9; for (int i = 0; i < limit; i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') temp++; if (s[i + 5] == 'm' && s[i + 6] == 'e' && s[i + 7] == 't' && s[i + 8] == 'a' && s[i + 9] == 'l') ans += temp; } } out.println(ans); //long runTime = System.currentTimeMillis() - startTime; //out.write(runTime + "\n"); out.close(); System.exit(0); } }
JAVA
318_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 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. Examples 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".
2
8
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */public final class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); String s = in.nextLine(); s = s.replace("heavy","1"); s = s.replace("metal","2"); long c = 0; long sum = 0; for(int i=s.length()-1;i>=0;i--){ if(s.charAt(i)=='2') c++; else if(s.charAt(i)=='1'){ sum+=c; } } System.out.println(sum); } }
JAVA
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long int c = 0; long long int ans = 0; for (long int i = 0; i < s.size(); i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { i += 4; c++; } else if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { i += 4; ans += c; } } cout << ans << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; import static java.lang.Math.*; public class Main extends Thread{ public static void main(String... args)throws IOException { new Main().start(); } String s; long ans=0,z=0; public void run() { MyReader in = new MyReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); s = in.nextString(); for(int i=0;i<s.length()-4;i++){ if(s.substring(i,i+5).equals("heavy"))z++; else if(s.substring(i,i+5).equals("metal"))ans+=z; } out.print(ans); in.close(); out.close(); } private class MyReader extends BufferedReader{ private StringTokenizer t; public MyReader(InputStreamReader in) { super(in); } int nextInt(){ return Integer.parseInt(nextString()); } long nextLong(){ return Long.parseLong(nextString()); } double nextDouble(){ return Double.parseDouble(nextString()); } String nextString(){ String out = ""; while(t==null||!t.hasMoreTokens()){ try { t = new StringTokenizer(this.readLine()); } catch (IOException e){ e.printStackTrace(); } } out=t.nextToken(); return out; } @Override public void close(){ try{ super.close(); } catch (IOException e){ e.printStackTrace(); } } } }
JAVA
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long i, h = 0, m = 0; for (i = 0; i < s.size(); i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { h++; } if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { m += h; } } cout << m; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; char str[1000010]; int h_sum; char str_h[] = "heavy", str_m[] = "metal"; int judge_h(int x) { int k = 0; for (int i = x; i < x + 5; i++) { if (str[i] == str_h[i - x]) k++; else break; } if (k == 5) return 1; else return 0; } int judge_m(int x) { int k = 0; for (int i = x; i <= x + 4; i++) { if (str[i] == str_m[i - x]) k++; else break; } if (k == 5) return 1; else return 0; } int main() { cin >> str; long long ans = 0; h_sum = 0; int length = strlen(str); for (int i = 0; i < length; i++) { if (str[i] == 'h') { if (judge_h(i)) { h_sum++; } } else if (str[i] == 'm') { if (judge_m(i)) { ans += h_sum; } } } cout << ans << endl; }
CPP
318_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 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. Examples 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".
2
8
L=list(input().split("heavy")) ans=0 for i in range(len(L)): ans += i*(len(list(L[i].split("metal")))-1) print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; import javax.print.attribute.standard.MediaSize.Other; public class Main { public static void main(String[] args) throws IOException { Main main = new Main(); main.read(); } /* IO */ private InputReader in; private StringBuilder ans; private String str; /* fields */ private void read() throws IOException { // streams boolean file = false; if (file) in = new InputReader(new FileInputStream("Input.txt")); else in = new InputReader(System.in); ans = new StringBuilder(); // read str = in.next(); str = str.replace("heavy", "1"); str = str.replace("metal", "2"); // solve System.out.println(solve()); } private long solve() { // get acc sum long sum = 0; int n = str.length(); long count[] = new long[n]; for (int i = 0; i < n; i++) if (str.charAt(i) == '1') count[i]++; for (int i = 1; i < n; i++) count[i] += count[i-1]; // find substrings for (int i = 0; i < n; i++) if (str.charAt(i) == '2') sum += count[i]; return sum; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } }
JAVA
318_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 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. Examples 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".
2
8
s = input() l = len(s) k = [ 0 for i in range(l) ] start = 0 while True: temp = s.find("heavy",start,l) if temp==-1: break else: k[temp] = 1 start = temp+1 start = 0 while True: temp = s.find("metal",start,l) if temp==-1: break else: k[temp] = 2 start = temp+1 sol = 0 ones = 0 for i in range(l): if k[i]==1: ones+=1 elif k[i]==2: sol+=ones print(sol)
PYTHON3
318_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 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. Examples 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".
2
8
import java.util.*; import java.io.*; public class d2_188_B { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); long ans=0 , count=0; ArrayList<Integer> list=new ArrayList<Integer>(); String s=sc.next(); for(int i=0;i<s.length()-4;i++) { if(s.substring(i, i+5).equals("heavy")) list.add(0); if(s.substring(i, i+5).equals("metal")) list.add(1); } for(int i=list.size()-1;i>=0;i--) { if(list.get(i)==1) count++; else ans+=count; } System.out.println(ans); } }
JAVA
318_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 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. Examples 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".
2
8
import sys right_metal = {'m': 0, 'e': 1, 't': 2, 'a': 3, 'l': 4} right_heavy = {'h': 0, 'e': 1, 'a': 2, 'v': 3, 'y': 4} metal_it = 4 metal_sub_it = 4 count_metal = 0 heavy_it = 4 heavy_sub_it = 4 total_count = 0 def find_substr(inp_str, start, end, rights, inner=True): global total_count match_count = 0 right_it = 4 edge = start last_heavy_count = 0 last_heavy_edge = 4 while start < end: while start >= 0 and inp_str[start] in rights and \ rights[inp_str[start]] == right_it: start -= 1 right_it -= 1 #print "start %i right %i" % (start, right_it) if right_it != -1: if inp_str[start] in rights: start += 4 - rights[inp_str[start]] else: start += 5 else: match_count += 1 if not inner: inner_count = find_substr(inp_str, last_heavy_edge, start + 1, right_heavy) last_heavy_count += inner_count last_heavy_edge = start + 1 total_count += last_heavy_count start += 8 if start < edge: start = edge + 5 edge = start right_it = 4 return match_count input_string = sys.stdin.readline() find_substr(input_string, 4, len(input_string), right_metal, False) print total_count
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; long long int c = 0; vector<int> v; int n = s.length(); for (int i = 0; i < n; i++) { if (s.substr(i, 5) == "metal") { v.push_back(i); } } int j = 0; for (int i = 0; i < n; i++) { if (s.substr(i, 5) == "heavy") { while (j < v.size() && v[j] < i) j++; if (j < v.size()) { c += (int)v.size() - j; } } } cout << c; return 0; }
CPP
318_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 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. Examples 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".
2
8
s = raw_input() total = 0 m = 0 c = len(s)-5 while(c >= 0): if(s[c:c+5] == 'metal'): m+=1 if(s[c:c+5] == 'heavy'): total+=m c -= 1 print total
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; inline void inpint(int &n) { n = 0; register int ch = getchar_unlocked(); bool sign = 0; while (ch < 48 || ch > 57) { if (ch == '-') sign = 1; ch = getchar_unlocked(); } while (ch >= 48 && ch <= 57) { n = (n << 3) + (n << 1) + ch - 48, ch = getchar_unlocked(); } if (sign) n = -n; } inline int sqr(int x) { return x * x; } inline int cube(int x) { return x * x * x; } inline long long sqrLL(long long x) { return x * x; } inline long long cubeLL(long long x) { return x * x * x; } const long long LLINF = 9223372036854775807LL; const long long LLINF17 = 100000000000000000LL; const int INF = 2147483647; const int INF9 = 1000000000; const int MOD = 1000000007; const double eps = 1e-7; const double PI = acos(-1.0); int dr[] = {1, 0, -1, 0, -1, 1, 1, -1}; int dc[] = {0, -1, 0, 1, 1, 1, -1, -1}; string s; vector<int> v1, v2; int main() { cin >> s; for (int(i) = (0); (i) < ((int)(s.length())); (i)++) { if (i + 4 < (int)(s.length()) && s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') v1.push_back(i); if (i + 4 < (int)(s.length()) && s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') v2.push_back(i); } long long ans = 0; for (int(i) = (0); (i) < ((int)(v1.size())); (i)++) { ans += ((int)(v2.size()) - (upper_bound(v2.begin(), v2.end(), v1[i]) - v2.begin())); } cout << ans << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; long long int cnt = 0, ans = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { ++cnt; } if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { ans += cnt; } } cout << ans; return 0; }
CPP
318_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 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. Examples 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".
2
8
temp = raw_input() '''x = temp.count('heavy') y = temp.count('metal') if(x==0 or y==0): print(0) exit(0)''' ls = ['h','e','a','v','y'] js = ['m','e','t','a','l'] m1 = [] m2 = [] ans = 0 sum = 0 i = 0 while i<(len(temp)-4): buf = '' check1 = False if(temp[i]=='h' or temp[i]=='m'): k=0 for j in range(i,i+5): if temp[j]==ls[k] or temp[j]==js[k]: buf += temp[j] else: break k+=1 if(buf=='heavy'): ans+=1 check1 = True if(buf=='metal'): sum+=ans check1 = True if(check1==True): i+=5 else: i+=1 print(sum)
PYTHON
318_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 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. Examples 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".
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { String s = in.readToken(); s = s.replaceAll("heavy", "1"); s = s.replaceAll("metal", "2"); long ones = 0; long ans = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') { ones++; } else if (s.charAt(i) == '2') { if (ones != 0) { ans += ones; } } } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == -1) { throw new InputMismatchException(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException var2) { throw new InputMismatchException(); } if (this.numChars <= 0) { return -1; } } return this.buf[this.curChar++]; } } public String readToken() { int c; while (isSpaceChar(c = this.read())) { ; } StringBuilder result = new StringBuilder(); result.appendCodePoint(c); while (!isSpaceChar(c = this.read())) { result.appendCodePoint(c); } return result.toString(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; } } }
JAVA
318_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 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. Examples 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".
2
8
string = raw_input() ans = 0 numb = string.split('heavy') for i, b in enumerate(numb): ans+=b.count('metal')*i print ans
PYTHON
318_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 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. Examples 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".
2
8
import io,os input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #deactivate when input contains string from collections import deque as que, defaultdict as vector from bisect import bisect as bsearch from heapq import* inin = lambda: int(input()) inar = lambda: list(map(int,input().split())) inst= lambda: input().decode().rstrip('\n\r') INF=float('inf') from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc _T_=1 #inin() for _t_ in range(_T_): s=inst() hv=0 ans=0 for i in range(len(s)-4): if s[i:i+5]=='heavy': hv+=1 if s[i:i+5]=='metal': ans+=hv print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
import sys s=raw_input().split('heavy') #splitsen bij woord 'heavy' res=0 for i in range(len(s)): res+=s[i].count('metal')*i print res
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6; char s[maxn + 100], *s1 = "heavy", *s2 = "metal"; int main() { long long h1 = 0, sum = 0; scanf("%s", s); int len = strlen(s); for (int i = 0; i < len - 4; i++) { int t1 = 0, t2 = 0; for (int j = 0; j < 5; j++) { if (s[i + j] == s1[j]) t1++; if (s[i + j] == s2[j]) t2++; } if (t1 == 5) h1++; if (t2 == 5) sum += h1; } cout << sum << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; template <class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } const int oo = int(1e9) + 7; const int dx[] = {1, 0, 0, -1}; const int dy[] = {0, -1, 1, 0}; const int N = int(2e5) + 10; long long ans = 0LL, c; string s; int main() { cin >> s; int n = s.length(); s = s + "......"; for (int i = n - 1; i >= 0; --i) { if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') c++; if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') ans += c; } cout << ans; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; queue<long long> heavy; queue<long long> metal; long long con; void heavys(string cad) { long long pos = cad.find("heavy"); while (pos != con) { heavy.push(pos); pos = cad.find("heavy", pos + 1); } } void metals(string cad) { long long pos = cad.find("metal"); while (pos != con) { metal.push(pos); pos = cad.find("metal", pos + 1); } } void procesar(string cad) { string aux = cad + "1"; con = cad.find(aux); heavys(cad); metals(cad); } int main() { long long res = 0, m, h; string entrada; getline(cin, entrada); procesar(entrada); while (!metal.empty() && !heavy.empty()) { h = heavy.front(); heavy.pop(); m = metal.front(); while (h > m && !metal.empty()) { metal.pop(); m = metal.front(); } res = res + metal.size(); } cout << res << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; long long int uz, q, D[1000005], B[1000005], y, at = 1, sayac; string a; map<long long int, long long int> mp; int main() { cin >> a; uz = a.size(); for (long long int i = 0; i <= uz - 1; i++) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') { mp[3]++; } if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') { sayac += mp[3]; } } printf("%lld", sayac); return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; string s; int main() { long long i, j, ans = 0; map<string, int> m; string s1 = "heavy", s2 = "metal"; cin >> s; long long n = s.size(); for (i = 0; i < n; i++) { string a = s.substr(i, 5); if (a == s1) m[s1]++; if (a == s2) { ans += m[s1]; } } cout << ans; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long int cnt = 0; long long int ans = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'h') { if (s[i + 1] == 'e') { if (s[i + 2] == 'a') { if (s[i + 3] == 'v') { if (s[i + 4] == 'y') cnt++; } } } } if (signal != 0 && s[i] == 'm') { if (s[i + 1] == 'e') { if (s[i + 2] == 't') { if (s[i + 3] == 'a') { if (s[i + 4] == 'l') { ans += cnt; } } } } } } cout << ans << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long cnt = 0, ans = 0; for (int i = 0; i < s.size(); i++) { if (s.substr(i, 5) == "heavy") cnt++; if (s.substr(i, 5) == "metal") ans += cnt; } cout << ans; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { int len, i; int pos = 0, pos1; long long sum = 0; int s = 0; int heavy[200000] = {0}; int metal[1000000] = {0}; char str[1000001]; scanf("%s", str); len = strlen(str); for (i = 0; i < len; i++) { if (str[i] == 'h' && str[i + 1] == 'e' && str[i + 2] == 'a' && str[i + 3] == 'v' && str[i + 4] == 'y') { heavy[pos++] = i; } { if (str[i] == 'm' && str[i + 1] == 'e' && str[i + 2] == 't' && str[i + 3] == 'a' && str[i + 4] == 'l') { s++; metal[i] = s; } else { metal[i] = s; } } } for (i = 0; i < pos; i++) { pos1 = heavy[i]; sum += s - metal[pos1]; } cout << sum; }
CPP
318_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 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. Examples 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".
2
8
a = input() if 'heavy'not in a: print(0) elif 'metal' not in a: print(0) elif len(a)<10: print(0) else: p=0 d=0 for k in range(len(a)-9): if d==0: if a[k:k+5]=='heavy': p+=a[k+5:].count('metal') s=a[k+5:].count('metal') d+=1 else: if a[k:k+5] =='heavy': p+=s elif a[k:k+5]=='metal': s-=1 print(p)
PYTHON3
318_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 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. Examples 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".
2
8
s = raw_input() i = 0 le = len(s) heavys = 0 metals = [] c = 0 while i < le - 4: if s[i] == 'h' and s[i+1] == 'e' and s[i+2] == 'a' and s[i+3] == 'v' and s[i+4] == 'y': heavys += 1 i += 4 elif s[i] == 'm' and s[i+1] == 'e' and s[i+2] == 't' and s[i+3] == 'a' and s[i+4] == 'l': c += heavys i += 4 else: i += 1 print c
PYTHON
318_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 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. Examples 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".
2
8
t = input() t = t.replace('heavy', '0') t = t.replace('metal', '1') a, b, s = 0, t.count('1'), 0 for i in t: if i == '0': a += 1 s += b elif i == '1': b -= 1 print(s)
PYTHON3
318_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 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. Examples 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".
2
8
a=input() b='' sh=0 sm=0 for i in range(0,len(a)-4): if a[i:i+5]=='heavy': sh+=1 if a[i:i+5]=='metal': sm+=sh print(sm)
PYTHON3
318_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 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. Examples 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".
2
8
s=input() a,b=0,0 n=len(s) for i in range(n): if(s[i:i+5]=='heavy'): a=a+1 elif(s[i:i+5]=='metal'): b=b+a print(b) #print the result
PYTHON3
318_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 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. Examples 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".
2
8
inp = input() if(len(inp)<10): print(0) exit(0) string=inp.split("heavy")[1:] count=0 counter=[None for i in range(len(string))] for i in range(len(string)): counter[i]=string[i].count("metal") count=counter[int(len(string)-1)] for i in range(len(string)-2,-1,-1): counter[i]+=counter[i+1] count+=counter[i] print(count)
PYTHON3
318_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 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. Examples 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".
2
8
//package com.deepak.PolyTest; import java.util.ArrayList; import java.util.Scanner; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { String st = new String(); String temp; st = sc.nextLine(); long len = st.length(),ct=0,ch=0,cm=0; ArrayList<Integer> a = new ArrayList<>(); for(int i=0;i<len;++i){ if(st.charAt(i)=='h'){ if(i+5<=len){ temp = st.substring(i,i+5); //System.out.println(temp); if(temp.equals("heavy")){ a.add(1); ch++; //i+=4; continue; } } } if(st.charAt(i)=='m'){ if(i+5<=len){ temp = st.substring(i,i+5); //System.out.println(temp); if(temp.equals("metal")){ a.add(2); cm++; //i+=4; } } } } for(int i:a){ if(i==1 && ch>0){ ct+=cm; ch--; } if(i==2){ cm--; } } System.out.println(ct); } }
JAVA
318_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 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. Examples 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".
2
8
word = input() n = len(word) a = 'heavy' b = 'metal' arr = [0 for i in range(n)] brr = [0 for i in range(n)] for i in range(n): if word[i:i+5]==a: arr[i]+=1 if word[i:i+5]==b: brr[i]+=1 for i in range(n-2,-1,-1): brr[i]+=brr[i+1] ans = 0 for i in range(n): ans+=arr[i]*brr[i] print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int ans = 0, t = 0; string s, a; cin >> s; for (long long int i = 0; i < s.length(); i++) { a = s.substr(i, 5); if (a == "heavy") t++; if (a == "metal") ans += t; } cout << ans; return 0; }
CPP
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; long long count = 0; stack<char> st; if (str.size() < 10) { cout << 0; return 0; } for (long long i = 0; i <= str.size() - 5; i++) { if (str[i] == 'h' && str[i + 1] == 'e' && str[i + 2] == 'a' && str[i + 3] == 'v' && str[i + 4] == 'y') { st.push('*'); } else if (str[i] == 'm' && str[i + 1] == 'e' && str[i + 2] == 't' && str[i + 3] == 'a' && str[i + 4] == 'l') { count = count + st.size(); } } cout << count << "\n"; }
CPP
318_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 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. Examples 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".
2
8
//package com.turtle.bone; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class AK375 { public static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub while (cin.hasNext()) { String str=cin.next(); long[] hmcount=new long[1000000]; long m=1; long l=str.length(); for(int i=0;i<str.length();i++){ if(str.charAt(i)=='h'){ if(i+5<=l&&str.substring(i,i+5).equals("heavy")){ hmcount[i+4]=-1; } }else if(str.charAt(i)=='m'){ if(i+5<=l&&str.substring(i,i+5).equals("metal")){ hmcount[i+4]=m; m++; } } } long res=0; for(int i=0;i<1000000;i++){ long count=1; if(hmcount[i]==-1){ int j; for(j=i+1;j<1000000;j++){ if(hmcount[j]!=0&&hmcount[j]!=-1){ res += (m-hmcount[j])*count; j--; break; }else if(hmcount[j]==-1){ count++; } } i=j; } } System.out.println(res); } } }
JAVA
318_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 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. Examples 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".
2
8
def main(): s=str(input()) s=s.replace("heavy","-") s=s.replace("metal","+") p,n=0,0 for k in s: if (k=="-"): n+=1 elif k=="+": p+=(n) print(p) main()
PYTHON3
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; vector<int> kmp(string s) { int n = s.length(); vector<int> pi(n, 0); vector<int> ans; for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) pi[i] = j + 1; if (pi[i] == 5) ans.push_back(i); } return ans; } int main() { string s; cin >> s; string a, b; a = "heavy#" + s; b = "metal#" + s; vector<int> h = kmp(a); vector<int> m = kmp(b); long long res = 0; for (int i = 0; i < h.size(); i++) { int p = h[i]; int k = upper_bound(m.begin(), m.end(), p) - m.begin(); if (k != -1) res += (m.size() - k); } cout << res << endl; return 0; }
CPP
318_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 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. Examples 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".
2
8
a = map(str, raw_input().split('heavy')) c = 0 b = 0 ans = 0 for i in a: ans += b * i.count('metal') b += 1 print ans
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; bool test(string f1, string f2) { if (f1.compare(f2) == 0) return true; return false; } int main() { string a, m = "metal", h = "heavy"; cin >> a; long long int sum = 0, temp = 0; for (int i = a.length() - 1; i >= 0; i--) { if (a[i] != 'y' && a[i] != 'l') continue; if (a[i] == 'l' && i >= 4) { if (test(m, a.substr(i - 4, 5))) { temp++; } } else if (i >= 4) { if (test(h, a.substr(i - 4, 5))) { sum += temp; } } } cout << sum; }
CPP
318_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 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. Examples 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".
2
8
from sys import stdin s = stdin.readline().strip('\r\n') s = s.replace('heavy','1'); s = s.replace('metal','2'); one = 0 two = 0 ans = 0 for i in range(len(s)): if(s[i] == '1'): one+=1 if(s[i] == '2'): ans+=one print ans
PYTHON
318_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 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. Examples 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".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long x = 0; long long ans = 0; for (long long i = 0; i < (int)s.length() - 4; i++) { if (s.substr(i, 5) == "heavy") x++; if (s.substr(i, 5) == "metal") ans += x; } cout << ans << "\n"; return 0; }
CPP
318_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 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. Examples 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".
2
8
#!/usr/bin/python def main(): result, lst = 0, raw_input().split('heavy') for i in range(len(lst)): result+=lst[i].count('metal')*i print result if __name__=='__main__': main()
PYTHON
318_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 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. Examples 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".
2
8
z=r=0 for w in input().split("heavy"):r+=w.count("metal")*z;z+=1 print(r) #JSR
PYTHON3
318_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 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. Examples 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".
2
8
import java.io.*; import java.util.*; public class test { public static void main(String[] argv){ Scanner in=new Scanner(System.in); String s=in.next(); int [] a=new int[200000]; int x=0; int count=0; while(x>=0&&x<s.length()){ x=s.indexOf("metal",x); if(x>=0) a[count++]=x; else break; x++; } a[count]=s.length(); x=0; int index=0; long ret=0; while(x>=0&&x<s.length()){ x=s.indexOf("heavy",x); if(x<0) break; while(x>a[index]) index++; ret+=count-index; x++; } System.out.println(ret); } }
JAVA
318_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 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. Examples 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".
2
8
import sys def fastio(): from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() def debug(*var, sep = ' ', end = '\n'): print(*var, file=sys.stderr, end = end, sep = sep) INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br s = input() ans = 0 heavy = metal = 0 n = len(s) for i in range(5, n + 1): if s[i - 5: i] == 'heavy': heavy += 1 elif s[i - 5: i] == 'metal': ans += heavy print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
import java.io.*; import java.util.*; import java.math.BigInteger; /* * This file was created by ayush in attempt to solve the problem problems */ public class main { public void solve(char[] inp, PrintWriter out) { long heavyCount =0; long ans =0; for(int i=0;i<inp.length-4;i++){ if(inp[i]=='h' && inp[i+1]=='e' && inp[i+2]=='a' && inp[i+3]=='v' && inp[i+4]=='y'){ heavyCount++; }else if(inp[i]=='m' && inp[i+1]=='e' && inp[i+2]=='t' && inp[i+3]=='a' && inp[i+4]=='l'){ ans+=heavyCount; } } out.println(ans); } public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); String inp = in.next(); Date t1 = new Date(); main solver = new main(); solver.solve(inp.toCharArray(), out); out.flush(); Date t2 = new Date(); System.err.println(String.format("Your program took %f seconds", (t2.getTime() - t1.getTime()) / 1000.0)); out.close(); } static class FastScanner { private static BufferedReader reader; private static StringTokenizer tokenizer; public FastScanner(InputStream in) throws Exception { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = new StringTokenizer(reader.readLine().trim()); } public int numTokens() throws Exception { if (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine().trim()); return numTokens(); } return tokenizer.countTokens(); } public boolean hasNext() throws Exception { if (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine().trim()); return hasNext(); } return true; } public String next() throws Exception { if (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine().trim()); return next(); } return tokenizer.nextToken(); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public float nextFloat() throws Exception { return Float.parseFloat(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public int[] nextIntArray() throws Exception { String[] line = reader.readLine().trim().split(" "); int[] out = new int[line.length]; for (int i = 0; i < line.length; i++) { out[i] = Integer.valueOf(line[i]); } return out; } public double[] nextDoubleArray() throws Exception { String[] line = reader.readLine().trim().split(" "); double[] out = new double[line.length]; for (int i = 0; i < line.length; i++) { out[i] = Double.valueOf(line[i]); } return out; } public Integer[] nextIntegerArray() throws Exception { String[] line = reader.readLine().trim().split(" "); Integer[] out = new Integer[line.length]; for (int i = 0; i < line.length; i++) { out[i] = Integer.valueOf(line[i]); } return out; } public BigInteger[] nextBigIngtegerArray() throws Exception { String[] line = reader.readLine().trim().split(" "); BigInteger[] out = new BigInteger[line.length]; for (int i = 0; i < line.length; i++) { out[i] = new BigInteger(line[i]); } return out; } public String nextLine() throws Exception { return reader.readLine().trim(); } public String readLine() throws Exception{ return tokenizer.nextToken(); } public BigInteger nextBigInteger() throws Exception { return new BigInteger(next()); } } static class DEBUG { public static void DebugInfo(String disp) { System.err.println("DEBUG Info: " + disp); } public static void DebugVariable(String variable, String value) { System.err.println("DEBUG Info: " + variable + " => " + value); } public static void DebugVariable(String variable, int value) { System.err.println("DEBUG Info: " + variable + " => " + value); } public static void DebugVariable(String variable, long value) { System.err.println("DEBUG Info: " + variable + " => " + value); } public static void DebugArray(int[] value) { for (int i = 0; i < value.length; i++) { System.err.println("DEBUG Info: " + i + " => " + value[i]); } } } }
JAVA
318_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 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. Examples 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".
2
8
from collections import defaultdict, deque, Counter, OrderedDict from bisect import insort, bisect_right, bisect_left import threading, sys def main(): s = input() ans, c = 0, 0 for i in range(0,len(s)-4): ss = s[i:i+5] if ss == "heavy": c += 1 elif ss == "metal": ans += c print(ans) if __name__ == "__main__": """sys.setrecursionlimit(400000) threading.stack_size(40960000)""" thread = threading.Thread(target=main) thread.start()
PYTHON3
318_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 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. Examples 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".
2
8
s = input() metals = [] heavys = [] ind = cur = -1 while True: cur = s.find('heavy', ind + 1) if cur != -1: heavys.append(cur) ind = cur else: break ind = cur = -1 while True: cur = s.find('metal', ind + 1) if cur != -1: metals.append(cur) ind = cur else: break ind = ans = 0 for x in metals: while ind < len(heavys) and heavys[ind] < x: ind += 1 ans += ind print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
subst = input().split('heavy') res = 0 for i in range(len(subst)): res += i * subst[i].count('metal') print(res)
PYTHON3
318_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 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. Examples 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".
2
8
import java.util.*; import java.util.regex.*; public class powerstring { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int x=0; long ans=0; for(int i=0;i<=s.length()-5;i++) { String temp = s.substring(i,i+5); if(temp.equals("heavy")) x++; else if(temp.equals("metal")) ans=ans+x; } System.out.println(ans); } }
JAVA
318_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 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. Examples 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".
2
8
from sys import stdin from collections import defaultdict as dd s=stdin.readline().rstrip() c=0 ans=0 for i in range(len(s)-4): if s[i:i+5]=="heavy": c+=1 elif s[i:i+5]=="metal": ans+=c print(ans)
PYTHON3
318_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 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. Examples 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".
2
8
s = raw_input() cnt = 0 ans = 0 for i in xrange(len(s)): cur = s[i - 4: i + 1] if cur == "heavy": cnt += 1 elif cur == "metal": ans += cnt print ans
PYTHON
318_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 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. Examples 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".
2
8
import java.util.Scanner; public class code { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String line = scan.nextLine(); long heavy = 0, ans = 0; for(int i = 0; i < line.length()-4; i++) if(line.substring(i, i+5).equals("heavy")) heavy++; else if(line.substring(i, i+5).equals("metal")) ans += heavy; System.out.println(ans); } }
JAVA