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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class HeavyMetal { public static void main (String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); ArrayList<Integer> hm = new ArrayList<Integer>(); for (int i = 0; i < input.length() - 4; i++){ char c = input.charAt(i); if (c == 'h'){ if (input.substring(i, i+5).equals("heavy")){ hm.add(0); } else { continue; } } else if (c == 'm'){ if (input.substring(i,i+5).equals("metal")){ hm.add(1); } else { continue; } } else { continue; } } int size = hm.size(); int[] h = new int[size]; int[] m = new int[size]; int n = 0; for (int j = 0; j < size; j++){ if (hm.get(j) == 0) { h[j] = 1; } else { h[j] = 0; n++; } m[j] = n; } long count = 0; for (int k = 0; k < size; k++){ count += h[k]*(n - m[k]); } 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
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 1; const long long mod = 1e9 + 7; const long long INF = 1e10; const long long M = (1 << 17) + 1; const long long LL = 1; string second; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> second; long long int cnt1 = 0, ans = 0; for (int i = 0; i < second.size(); i++) { string st = second.substr(i, 5); if (st == "heavy") ++cnt1; else if (st == "metal") ans += cnt1; } cout << ans; }
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; const int N = 1000010; const char sx[] = "heavy", sy[] = "metal"; char a[N]; int l, x, y; long long cnt, ans; inline bool check1(int pos) { for (int i = pos, j = 0; j < 5; i++, j++) if (a[i] != sx[j]) return false; return true; } inline bool check2(int pos) { for (int i = pos, j = 0; j < 5; i++, j++) if (a[i] != sy[j]) return false; return true; } int main() { scanf("%s", a + 1); l = strlen(a + 1); for (int i = 1; i <= l - 4; i++) { if (check1(i)) cnt++; if (check2(i)) ans += cnt; } 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
sentence = raw_input() sentence = sentence.replace('heavy','+') sentence = sentence.replace('metal','-') num = 0 result = 0 for char in sentence: if char=='+': num = num + 1 if char=='-': result+=num print result
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() c=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
#include <bits/stdc++.h> using namespace std; int main() { string a; vector<long long int> x, y; cin >> a; long long int n = a.size(); for (long long int i = 0; i < n - 4; i++) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') x.push_back(i); else if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') y.push_back(i); } sort(x.begin(), x.end()); long long int cnt = 0; for (long long int i = 0; i < y.size(); i++) { cnt += (lower_bound(x.begin(), x.end(), y[i]) - x.begin()); } cout << cnt; }
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() i, heavy, count = 0, 0, 0 while i < len(s): 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)
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; string s; bool is_heavy(int i) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') return 1; return 0; } bool is_metal(int i) { if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') return 1; return 0; } int main() { long long m = 0, res = 0; cin >> s; for (int i = s.length() - 5; i >= 0; i--) { if (is_metal(i)) m++; else if (is_heavy(i)) res += m; } cout << res << 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
s=input() h=[] m=[] for i in range(len(s)-4): if(s[i:i+5]=='heavy'): h.append(i) for i in range(len(s)-4): if(s[i:i+5]=='metal'): m.append(i) l=0 r=0 total=0 while(l<len(h) and r<len(m)): if(m[r]>h[l]): total+=len(m)-r l+=1 else: r+=1 print(total)
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 re s = raw_input() n = len(s) c = [0] * n for m in re.finditer("heavy", s): c[m.start()] += 1 for i in xrange(n - 1): c[i + 1] += c[i] res = 0 for m in re.finditer("metal", s): res += c[m.start()] 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
/** * Created with IntelliJ IDEA. * User: akashm * Date: 6/15/13 * Time: 12:08 AM * To change this template use File | Settings | File Templates. */ import java.util.*; public class B318 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); ArrayList<Integer> metal = bruteForce(s, "metal"); ArrayList<Integer> heavy = bruteForce(s, "heavy"); int pos = 0; long count = 0; // System.out.println(heavy); // System.out.println(metal); for (int i = 0; i < heavy.size(); i++) { count += metal.size() - ((Collections.binarySearch(metal, heavy.get(i)) * -1) - 1); } System.out.println(count); metal.clear(); heavy.clear(); } private static ArrayList<Integer> bruteForce(String input, String string) { ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < input.length(); i++) { int j = 0; for (j = 0; j < string.length() && i + j < input.length(); j++) if (input.charAt(i + j) != string.charAt(j)) break; if (j == string.length()) list.add(i); } return list; } }
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.Arrays; import java.util.Scanner; import java.lang.String; import java.lang.reflect.Array; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); char[] p = s.toCharArray(); ArrayList<String> list = new ArrayList<String>(); long cm = 0, ch = 0; for (int i = 0; i < p.length; i++) { if (p[i] == 'h') { ch = 1; } else if (p[i] == 'e' && ch == 1) { ch = 2; } else if (p[i] == 'a' && ch == 2) { ch = 3; } else if (p[i] == 'v' && ch == 3) { ch = 4; } else if (p[i] == 'y' && ch == 4) { ch = 0; list.add("1"); } else { ch = 0; } if (p[i] == 'm') { cm = 1; } else if (p[i] == 'e' && cm == 1) { cm = 2; } else if (p[i] == 't' && cm == 2) { cm = 3; } else if (p[i] == 'a' && cm == 3) { cm = 4; } else if (p[i] == 'l' && cm == 4) { cm = 0; list.add("2"); } else { cm = 0; } } long counter1 = 0, sum = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i) == "1") { counter1++; } else { sum += counter1; } } System.out.print(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
s=input() ans=0 s=s.replace("heavy","1") s=s.replace("metal","2") heavy=[] metal=[] n=len(s) for i in range(n): if(s[i]=="1"): heavy.append(i) elif(s[i]=="2"): metal.append(i) n=len(heavy) nn=len(metal) x=0 l=nn for item in heavy: for i in range(x,nn): if(metal[i]>item): ans+=(nn-i) l=i break x=l; 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
str1 = input() heavycount = 0 sol = 0 i = 0 while i < len(str1)-4: if str1[i] == 'h': if str1[i:i+5] == 'heavy': heavycount += 1 if str1[i] == 'm': if str1[i:i+5] == 'metal': sol += heavycount i+=1 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.ArrayList; import java.util.Arrays; import java.util.Scanner; public class B138 { public static void main(String[] args){ Scanner in = new Scanner(System.in); String s = in.nextLine(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); int i = 0; while(true){ i = s.indexOf("heavy",i); if(i>-1){ a.add(i); i=i+5; }else break; } i = 0; while(true){ i = s.indexOf("metal",i); if(i>-1){ b.add(i); i=i+5; }else break; } Integer[] c = new Integer[a.size()]; Integer[] d = new Integer[b.size()]; long count = 0,indexOf; c = a.toArray(c); d = b.toArray(d); i=0; while(i<c.length){ indexOf = Arrays.binarySearch(d,c[i]); count = count +(d.length + indexOf + 1); i++; } 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 com.sun.org.apache.xalan.internal.xsltc.dom.ClonedNodeListIterator; import sun.nio.cs.KOI8_U; import java.io.*; import java.util.*; import java.math.*; import java.lang.*; public class AD implements Runnable { public void run() { InputReader sc = new InputReader(); PrintWriter out = new PrintWriter(System.out); int i=0,j=0,k=0; int t=0; //t=sc.nextInt(); for(int testcase = 0; testcase < t; testcase++) { } String str=sc.next(); String heavy="heavy"; String metal="metal"; int strlen=str.length(); long ans=0; int heavycount=0; for (i=0;i<strlen-4;i++) { String temp=str.substring(i,i+5); //out.println(str.substring(i,i+5)); if (temp.equals(heavy)) { heavycount++; } else if (temp.equals(metal)) { ans+=heavycount; } } out.println(ans); //================================================================================================================================ out.flush(); out.close(); } //================================================================================================================================ public static int[] sa(int n,InputReader sc) { int arr[]=new int[n]; for (int i=0;i<n;i++) arr[i]=sc.nextInt(); return arr; } public static long gcd(long a,long b){ return (a%b==0l)?b:gcd(b,a%b); } private static long lcm(long a, long b) { return a * (b / gcd(a, b)); } public int egcd(int a, int b) { if (a == 0) return b; while (b != 0) { if (a > b) a = a - b; else b = b - a; } return a; } public int countChar(String str, char c) { int count = 0; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == c) count++; } return count; } static int binSearch(Integer[] arr, int number){ int left=0,right=arr.length-1,mid=(left+right)/2,ind=0; while(left<=right){ if(arr[mid]<=number){ ind=mid+1; left=mid+1; } else right=mid-1; mid=(left+right)/2; } return ind; } static int binSearch(int[] arr, int number){ int left=0,right=arr.length-1,mid=(left+right)/2,ind=0; while(left<=right){ if(arr[mid]<=number){ ind=mid+1; left=mid+1; } else right=mid-1; mid=(left+right)/2; } return ind; } static class Pair { int a,b; Pair(int aa,int bb) { a=aa; b=bb; } String get() { return a+" "+b; } String getrev() { return b+" "+a; } } static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } static long factorial(long n) { if (n == 0) return 1; return n*factorial(n-1); } //================================================================================================================================ static 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; } } public static void main(String args[]) throws Exception { new Thread(null, new AD(),"Main",1<<27).start(); } }
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.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner r=new Scanner(new InputStreamReader(System.in)); String a=r.nextLine(); long l=a.length()-4,h=0,o=0; for(int j=0;j<l;j++){ String t=a.substring(j,j+5); if(t.equals("heavy")){h++;j+=4;} else if( t.equals("metal")){ j+=4; o+=h; } } System.out.println(o); } }
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
#kisi "metal" substring ke pehle jitne bhi "heavy" substring aayenge #wo utna count badhayega... nd so on... # s=raw_input() sub="aaaaa" count=a=0 for x in s: sub=sub[1:]+x if sub=="heavy": a+=1 if sub=="metal": count+=a 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // String of Power : Codeforces // status : WA public class StringPower { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String s = bf.readLine(); int n = s.length(); long H = 0; long cont = 0; if(n < 5) { System.out.println(0); } else { for(int i = 0; i <= n - 5; i++) { String S = s.substring(i,i+5); if(S.equals("heavy")) { H += 1; i += 4; } if(S.equals("metal")) { cont += H; i += 4; } //System.out.println(S); } System.out.println(cont); } } }
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
print(sum(i * s.count('metal') for i, s in enumerate(input().split('heavy'))))
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> heavies; vector<int> metals; int Greaters(int left, int right, int input) { int ans = 0; if (left == right - 1) { if (input <= metals[left]) { ans++; } return ans; } int mid = (left + right) / 2; if (metals[mid] >= input) { ans += right - mid; ans += Greaters(left, mid, input); } else { ans += Greaters(mid, right, input); } return ans; } int main() { string s; cin >> s; int n = s.size(); for (int i = 0; i < n - 4; i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { heavies.push_back(i); } if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { metals.push_back(i); } } long long ans = 0; if (metals.size() > 0) { for (int i = 0; i < heavies.size(); i++) { ans += Greaters(0, metals.size(), heavies[i]); } } 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
import sys def solve(string): str1 = 'heavy' str2 = 'metal' start = string.find(str1) end = string.rfind(str2) if start == -1 or end == -1: return 0 string = string[start+len(str1):end] data = {-1:True, len(string): False} pos = -1 cnt1 = 1 while True: pos = string.find(str1, pos+1) if pos == -1: break else: data[pos] = True cnt1 += 1 pos = -1 cnt2 = 1 while True: pos = string.find(str2, pos+1) if pos == -1: break else: data[pos] = False cnt2 += 1 #data.sort() bida = False cnt = 0 ind = 0 last_ind = 0 for key in sorted(data): ind += 1 if bida: if data[key] == True: cnt += (ind - last_ind) last_ind += 1 else: if data[key] == False: bida = True last_ind = ind #print str(key) + '->' + str(data[key]) #return string #print '---' #print cnt1 #print cnt2 #print cnt return cnt1 * cnt2 - cnt string = sys.stdin.readline() print solve(string)
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
st=input() l=len(st) h=0 c=0 for i in range(0,l): if((i+5)<=l): if(st[i:i+5]=="heavy"): h=h+1 elif(st[i:i+5]=="metal"): c=c+h 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
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ //package pkg188; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; /** * * @author Alik */ public class B { static String line; static int cur = 0; public static void main(String[] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); line = in.readLine(); int a[] = new int [1000000], size = 0; for (int i = 0; i < line.length() - 4; i++) if (line.substring(i, i + 5).equals("heavy")) { a[size] = i; size++; } //System.out.println(a[0] +" %%%%"+ a[1] + "%%%%%%" + a[2]); long ans = 0; int v = 0; for (int i = 0; i < line.length() - 4; i++) { if (line.substring(i, i + 5).equals("metal")) { long q = v; ans += q; //System.out.println(i); } if (i > a[v] && v < size) v++; } System.out.println(ans); } private static int sp() { int x = 0; while (cur < line.length()) { if (line.charAt(cur) == ' ') break; x = x * 10 + (line.charAt(cur) - 48); cur++; } cur++; if (cur >= line.length()) cur = 0; return x; } }
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
L = "heavy" R = "metal" D = 0 AL = 0 # drop until first L # if no L then return D = 0 # split rest by L # set some AL to 0 # in [1, ..] pieces each # x = count R # add x to D # multiply x and AL and add to D # increment AL # return D s = raw_input() drop_index = s.find(L) if drop_index != -1: rest = s[drop_index:] pieces = rest.split(L)[1:] for subs in pieces: x = subs.count(R) D += x D += AL * x AL += 1 print D
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)
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
st = input() p = 0 ans = 0 for i in range(len(st) - 4): if st[i:i+5] == 'heavy': p += 1 if st[i:i+5] == 'metal': ans += p 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 = input().strip() ans = 0 head = 0 for i in range(4, len(s)): if s[i - 4: i + 1] == "heavy": head += 1 if s[i - 4: i + 1] == 'metal': ans += head 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() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s1; cin >> s1; long long heavy_count = 0, ans = 0; if (s1.size() > 5) { for (int i = 0; i < s1.size() - 5 + 1; i++) { if (s1[i] == 'h' && s1[i + 1] == 'e' && s1[i + 2] == 'a' && s1[i + 3] == 'v' && s1[i + 4] == 'y') { heavy_count++; } else if (s1[i] == 'm' && s1[i + 1] == 'e' && s1[i + 2] == 't' && s1[i + 3] == 'a' && s1[i + 4] == 'l') { ans += heavy_count; } } } cout << ans; }
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.Scanner; public class B { /** * @param args */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); char[]s = sc.next().toCharArray(); long ans=0; long heavy=0; for(int i=0;i<s.length;i++){ if(isHeavy(s,i)){ heavy++; } else if(isMetal(s,i)){ ans += heavy; } } System.out.println(ans); } static boolean isHeavy(char[]s ,int idx){ if(idx+5 > s.length)return false; if(s[idx]=='h'&& s[idx+1]=='e'&& s[idx+2]=='a'&& s[idx+3]=='v'&& s[idx+4]=='y'){ return true; } return false; } static boolean isMetal(char[]s ,int idx){ if(idx+5 > s.length)return false; if(s[idx]=='m'&& s[idx+1]=='e'&& s[idx+2]=='t'&& s[idx+3]=='a'&& s[idx+4]=='l'){ return true; } return false; } }
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 str; while (cin >> str) { long long int sum, i, temp; sum = temp = 0; for (i = 0; i < str.size(); i++) { if (str[i] == 'h' && str[i + 1] == 'e' && str[i + 2] == 'a' && str[i + 3] == 'v' && str[i + 4] == 'y') { i = i + 4; temp++; } if (str[i] == 'm' && str[i + 1] == 'e' && str[i + 2] == 't' && str[i + 3] == 'a' && str[i + 4] == 'l') { i = i + 4; sum = sum + temp; } } cout << sum << 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 java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Trung Pham */ public class B { public static void main(String[] args) { Scanner in = new Scanner(); PrintWriter out = new PrintWriter(System.out); String val = in.next(); int[] h = new int[val.length()]; int[] m = new int[val.length()]; int i = 0, j = 0; String a = "heavy"; String b = "metal"; for (int k = 0; k < val.length(); k++) { if (k > 0) { h[k] = h[k - 1]; m[k] = m[k - 1]; } if (val.charAt(k) == a.charAt(i)) { i++; if (i == a.length()) { i = 0; h[k]++; } } else { if (val.charAt(k) == 'h') { i = 1; } else { i = 0; } } if (val.charAt(k) == b.charAt(j)) { j++; if (j == b.length()) { j = 0; m[k]++; } } else { if (val.charAt(k) == 'm') { j = 1; } else { j = 0; } } } long result = 0; for (int k = 1; k < h.length; k++) { if (h[k] > h[k - 1]) { // System.out.println(k); result += m[h.length - 1] - m[k]; } } out.println(result); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { //System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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 cf318B{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); String inp = sc.next(); int startIndx = 0; long count=0,heavy_cnt =0; int heavy_index = 0; int metal_index = 0; int heavy_index_old = 0; int metal_index_old = 0; //ArrayList<Integer> heavy = new ArrayList<Integer>(2); while(heavy_index >=0 && metal_index >=0 && startIndx < inp.length()) { if(startIndx==0) { heavy_index = inp.indexOf("heavy", startIndx); metal_index = inp.indexOf("metal", startIndx); heavy_cnt++; if(heavy_index >=0 && metal_index >=0) if(heavy_index > metal_index) { startIndx = metal_index + 1;} else { startIndx = heavy_index+1; count++;} } else if(heavy_index > metal_index) { metal_index_old = metal_index; metal_index = inp.indexOf("metal", startIndx); if(heavy_index >=0 && metal_index >=0) if(metal_index > heavy_index) { count = count+heavy_cnt; startIndx = heavy_index+1; } else { count = count + heavy_cnt -1; startIndx = metal_index+1; } } else { heavy_index_old = heavy_index; heavy_index = inp.indexOf("heavy", startIndx); if(heavy_index >=0 && metal_index >=0) if(metal_index > heavy_index) { count = count+1; startIndx = heavy_index+1; heavy_cnt++; } else { startIndx = metal_index+1; heavy_cnt++; } } } if(metal_index >=0 && heavy_index <0) { startIndx = metal_index+1; while(startIndx<inp.length()) { metal_index = inp.indexOf("metal", startIndx); if(metal_index <0) break; if(metal_index > heavy_index_old) { count = count+heavy_cnt; startIndx = metal_index+1; } else { count = count + heavy_cnt -1; startIndx = metal_index+1; } } } else if(metal_index <0 && heavy_index >=0) { startIndx = heavy_index+1; while(startIndx<inp.length()) { heavy_index = inp.indexOf("heavy", startIndx); if(heavy_index <0 ) break; else if(metal_index_old > heavy_index) { count = count+1; startIndx = heavy_index+1; heavy_cnt++; } else { startIndx = heavy_index+1; heavy_cnt++; } } } 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
a=input() b=0 c=0 for i in range(len(a)-4): if a[i:i+5]=='heavy':b+=1 if a[i:i+5]=='metal':c+=b 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
import java.util.*; import java.io.*; public class metalheavy{ public static void main(String args[]) throws IOException{ BufferedReader lector = new BufferedReader(new InputStreamReader(System.in)); String tmp = lector.readLine(); long a = 0; long b = 0; for(int n =0;n<=tmp.length()-5;n+=5){ if(tmp.substring(n,n+5).equals("heavy")) a++; else if(tmp.substring(n,n+5).equals("metal")) b+=a; else n-=4; } System.out.println(b); } }
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() { char a[1000000]; cin >> a; if (strlen(a) < 5) { cout << 0 << endl; return (0); } long long i, j = 0, l, ans = 0; for (i = 0; i < strlen(a) - 4; i++) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') j++; if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') ans += j; } 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; int N = s.size(); long long heavy = 0, sum = 0; for (int i = 0; i < N - 4; i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') heavy++; else if (heavy > 0 && s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') sum += heavy; } 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; int main() { string a; cin >> a; long long int i, length, cnt = 0, res = 0; length = a.size(); for (i = 0; i < length; i++) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') cnt++; else if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') res += cnt; } 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; int main() { string str; while (cin >> str) { long long sum = 0, heavy = 0; string temp; for (int i = 0; i < str.size(); i++) { temp = str.substr(i, 5); if (temp == "heavy") heavy++, i += 4; else if (temp == "metal") sum += heavy, i += 4; } cout << sum << 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 java.util.Scanner; public class StringsOfPower { static char[] arr; public static void main(String[] args) { Scanner sc = new Scanner(System.in); // arr = sc.nextLine().toCharArray(); String s = sc.nextLine(); s = s.replaceAll("metal", ","); s = s.replaceAll("heavy", "."); long add = 0; long result = 0; for (int i = 0; i < s.length(); i++) { if(s.charAt(i) == '.') { add++; // i += 4; } else if(s.charAt(i) == ',') { result += add; // i+=4; } } System.out.println(result); } public static boolean heavy(int i) { if(arr.length - i < 5) return false; else if(arr[i] == 'h' && arr[i+1] == 'e' && arr[i+2] == 'a' && arr[i+3] == 'v' && arr[i+4] == 'y') return true; else return false; } public static boolean metal(int i) { if(arr.length - i < 5) return false; else if(arr[i] == 'm' && arr[i+1] == 'e' && arr[i+2] == 't' && arr[i+3] == 'a' && arr[i+4] == 'l') return true; else return false; } }
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(int argc, char** argv) { string s; unsigned long long n = 0; cin >> s; unsigned long long cnt = 0; size_t pos = 0; while (pos < s.size()) { if (s.compare(pos, 5, "heavy") == 0) { cnt++; pos += 5; } else if (s.compare(pos, 5, "metal") == 0) { n += cnt; pos += 5; } else pos++; } cout << n << 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=raw_input() n=len(s) looper=0 ans=0 for i in range(n-4): if s[i:i+5]=="heavy": looper+=1 elif s[i:i+5]=="metal": ans+=looper 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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.util.*; import java.math.*; import java.io.*; /** * * @author magzhan */ public class Cf { public final static boolean isPerfectSquare(long n) { if (n < 0) return false; switch((int)(n & 0xF)) { case 0: case 1: case 4: case 9: long tst = (long)Math.sqrt(n); return tst*tst == n; default: return false; } } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here try { Scanner in = new Scanner(System.in); String s = in.next(); String metal = "metal", heavy = "heavy"; Vector<Integer> d = new Vector<Integer>(); long c = 0, t = 0, e = 0; for (int i = 0; i <= s.length() - metal.length(); i++) { String p = s.substring(i, i + metal.length()); if (p.equals(heavy)) { c++; } else if (p.equals(metal)) { t += c; } } System.out.println(t); } catch(Exception ex) { System.err.println(ex.toString()); } } }
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.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class test { static int MAX = 1000001; public static void main(String[] argvs) throws Exception{ FastScanner scan = new FastScanner(System.in); String in = scan.next(); int i = 0; int cnt[] = new int[MAX]; int count = 0; while(true){ int find = in.indexOf("heavy", i); if(find == -1) { for(int j = i; j < in.length(); ++j){ cnt[j] = count; } break; } for(int j = i; j < find; ++j){ cnt[j] = count; } ++count; i = find + 1; } i = 0; long ret = 0; while(true){ int find = in.indexOf("metal", i); if(find == -1) break; ret += cnt[find]; i = find + 1; } System.out.println(ret); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) throws Exception { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(br.readLine().trim()); } public int numTokens() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine().trim()); return numTokens(); } return st.countTokens(); } public boolean hasNext() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine().trim()); return hasNext(); } return true; } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine().trim()); return next(); } return st.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 String nextLine() throws Exception { return br.readLine(); } } }
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() { char Str[1000005] = {}; cin >> Str; long long len = strlen(Str); long long Total = 0; long long Count = 0; for (long long i = 0; i < len;) { if ((Str[i] == 'h') && (Str[i + 1] == 'e') && (Str[i + 2] == 'a') && (Str[i + 3] == 'v') && (Str[i + 4] == 'y')) { Total++; i = i + 5; } else if ((Str[i] == 'm') && (Str[i + 1] == 'e') && (Str[i + 2] == 't') && (Str[i + 3] == 'a') && (Str[i + 4] == 'l')) { Count += Total; i = i + 5; } else { i++; } } cout << Count << 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
# coding=utf-8 print(sum(i * s.count('metal') for i, s in enumerate(input().split('heavy'))))
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; string s; long long ans, numo; int main() { cin >> s; if (s.length() < 10) { cout << 0; return 0; } for (int i = 0; i < s.length() - 4; ++i) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { ++numo; } if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { ans += numo; } } 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
def main(): l = [] for s in input().split('heavy')[1:]: l.append(0) for ss in s.split('metal'): l.append(1) del l[-1] res = a = 0 for t in l: if t: res += a else: a += 1 print(res) if __name__ == '__main__': 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; #pragma warning(disable : 4996) string s, s1; int p[1100000]; int e1[200010], e2[200010], l1, l2; long long r; int n; void sub() { int k = 0; p[0] = 0; for (int i = 1; i < n; i++) { while (k > 0 && s[i] != s[k]) k = p[k - 1]; if (s[i] == s[k]) k++; p[i] = k; }; } int main() { cin >> s1; s = "heavy#" + s1; n = s.size(); sub(); for (int i = 0; i < n; i++) if (p[i] == 5) { l1++; e1[l1] = i; } s = "metal#" + s1; n = s.size(); sub(); for (int i = 0; i < n; i++) if (p[i] == 5) { l2++; e2[l2] = i; } e2[l2 + 1] = 1000000000; int pp = 1; for (int i = 1; i <= l1; i++) { while (e2[pp] < e1[i]) pp++; if (pp == l2 + 1) break; r += (long long)l2 - (long long)pp + (long long)1; }; cout << r << 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 sys import fractions import string a = raw_input() a = a.replace('heavy', '0') a = a.replace('metal', '1') current = a.count('1') ret = 0 for c in a: if c == '0': ret += current elif c == '1': current -= 1 print ret
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; char s[1000010]; int ss[1000010]; int main() { int u = 0, v = 4, k = 0, l = 0, r = 0; int c = 0, d = 0; long long sum = 0; char s1[] = {"heavy"}; char s2[] = {"metal"}; scanf("%s", s); int p = strlen(s); while (v <= p - 1) { for (int i = u, j = 0; i <= v; i++, j++) { if (s[i] != s1[j]) c = 1; } for (int i = u, j = 0; i <= v; i++, j++) { if (s[i] != s2[j]) d = 1; } if (c == 0) { ss[k] = 1; k++; l++; } if (d == 0) { k++; r++; } c = 0; d = 0; u++; v++; } for (int i = 0; i < k; i++) { if (ss[i] == 1) { sum += r; } else r--; } printf("%lld\n", sum); 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
st=raw_input() heavy=0 count=0 for i in range(len(st)-4): if st[i:i+5] == 'heavy': heavy+=1 if st[i:i+5] == 'metal': count+=heavy 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
import java.util.Scanner; public class Codeforce { public static void main(String [] args){ Scanner in = new Scanner(System.in); String s = in.nextLine(); String temp; long ans=0, k=0; for(int i=0; i<s.length()-4; i++){ temp = s.substring(i,i+5); if(temp.equals("heavy")){ k++; i+=4; } if(temp.equals("metal")){ ans+=k; i+=4; } } 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 java.util.*; import java.io.*; public class b188 { public static void main(String args[]) { String text; Scanner sc = new Scanner(System.in); text= sc.next(); int number=0; int heavyindex=0; int metalindex=0; int totalheavy=0; long totalmetal=0; int lastheavy = text.indexOf("heavy"); if(text.length()<5 || lastheavy==-1) System.out.println(0); else{ //System.out.println("lastheavy is "+lastheavy); String text1=text.substring(lastheavy+5); //System.out.println("text1 is "+text1); totalmetal = text1.split("metal", -1).length-1; //System.out.println("totalmetal is "+totalmetal); long totalnewmetal = totalmetal; while(text1.indexOf("heavy")!=-1 && totalmetal>=1) { //System.out.println("text1 is "+text1); int heavy = text1.indexOf("heavy"); //System.out.println("heavy is "+heavy); String sub = text1.substring(0,heavy+5); //System.out.println("sub is "+sub); totalnewmetal = totalnewmetal-(sub.split("metal", -1).length-1); //System.out.println("totalnewmetal is "+totalnewmetal); totalmetal = totalmetal+totalnewmetal; lastheavy = heavy; text1=text1.substring(lastheavy+5); } //if(f==1) System.out.println(totalmetal); //else System.out.println(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
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int a[N], b[N]; char s[N]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++) { if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') a[i] = 1; } for (int i = n; i >= 1; i--) a[i] += a[i + 1]; long long ans = 0; for (int i = 1; i <= n; i++) if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') ans += a[i]; 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
s=raw_input() t,r=0,0 for i in range(5,len(s)+1): t+=(s[i-5:i]=='heavy') r+=t*(s[i-5:i]=='metal') print r
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 cnt, ans, i, j, temp, flag; string s, heavy = "heavy", metal = "metal"; int main() { cin >> s; for (i = 0; i < s.length(); i++) { temp = i; flag = 0; for (j = 0; j < 5; j++) { if (s[temp] == heavy[j]) ++temp; else break; } if (j == 5) flag = 1, ++cnt; if (flag) { i = temp - 2; continue; } temp = i; for (j = 0; j < 5; j++) { if (s[temp] == metal[j]) ++temp; else break; } if (j == 5) flag = 1, ans += cnt; if (flag) { i = temp - 2; continue; } } cout << ans; }
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 h[1001000]; int m[1001000]; string s; int main() { cin >> s; int t1 = 0; int t2 = 0; for (int i = 0; i < (signed)s.length() - 4; i++) { if (s.substr(i, 5) == "heavy") h[t1++] = i; if (s.substr(i, 5) == "metal") m[t2++] = i; } long long resp = 0; int pa = 0, pb = 0; while (pa != t1) { if (h[pa] < m[pb] || pb == t2) { resp += (t2 - pb); pa++; } else { pb++; } } cout << resp << 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 java.io.*; import java.util.*; public class Woodcutters { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { //Reader ob=new Reader(); Scanner ob=new Scanner(System.in); String s=ob.next(); long ans=0, c=0; for(int i=0;i<s.length()-4;i++) { if(s.substring(i,i+5).equals("heavy")) c++; if(s.substring(i,i+5).equals("metal")) ans+=c; } 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
ans = 0 s = raw_input() heavy = 0 i = 0 if len(s) >= 5: while i < len(s) - 4: if s[i:i+5] == 'heavy': heavy += 1 i += 5 elif s[i:i+5] == 'metal': ans += heavy i += 5 else: i += 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
z=r=0 for w in input().split("heavy"):r+=w.count("metal")*z;z+=1 print(r)
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
string = input() n = len(string) havyMetals = [] for i in range(n): if i < n - 4: if string[i:i+5] in ["heavy", "metal"]: havyMetals.append(string[i:i+5]) counts = [[0, 0]] for i in range(len(havyMetals)): if havyMetals[i] == "heavy": counts.append([counts[-1][0] + 1, counts[-1][1]]) else: counts.append([counts[-1][0], counts[-1][1] + 1]) counts.pop(0) if len(counts) == 0: print(0) exit() heavys = counts[-1][0] metals = counts[-1][1] total = 0 for i in range(len(havyMetals)): if havyMetals[i] == "heavy": total += metals - counts[i][1] print(total)
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 re line = raw_input().strip() heavy = [(m.start(), 'h') for m in re.finditer('heavy', line)] metal = [(m.start(), 'm') for m in re.finditer('metal', line)] ls = heavy + metal ls.sort(key=lambda x: x[0]) metal_count = len(metal) cur_metal_count = metal_count ans = 0 for x in ls: if x[1] == 'h': ans += cur_metal_count else: cur_metal_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
import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.Scanner; public class ProblemB { public static void main(String[] args) { ProblemB problem = new ProblemB(); try { problem.solve(System.in, System.out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void solve(InputStream in, PrintStream out) throws IOException { int x = in.read(); long heavyCount = 0; long metalCount = 0; int heavyIndex = 0; int metalIndex = 0; long result = 0; char[] heavy = {'h', 'e', 'a', 'v', 'y' }; char[] metal = {'m', 'e', 't', 'a', 'l' }; while ( x != - 1) { if ( x == heavy[heavyIndex]) { heavyIndex++; if (heavyIndex == heavy.length) { heavyIndex = 0; heavyCount++; } } else if ( x == heavy[0]) { heavyIndex = 1; } else heavyIndex = 0; // metal if ( x == metal[metalIndex]) { metalIndex++; if (metalIndex == metal.length) { metalIndex = 0; metalCount++; result += heavyCount; } } else if ( x == metal[0]) { metalIndex = 1; } else metalIndex = 0; x = in.read(); } out.println(result); } }
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 x = -5; long long sum = 0; long long a = 0; long long INF = s.length() + 5; long long z = -1, y = -1; while (x + 5 < s.length()) { long long z1 = z, y1 = y; if (z1 <= y1) z = s.find("heavy", x + 5); if (z1 >= y1) y = s.find("metal", x + 5); if (z == -1) z = INF; if (y == -1) y = INF; if (z < y) a++; else if (z > y) sum += a; if (z == INF && y == INF) break; x = min(z, y); } cout << sum << 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 java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.InputMismatchException; public class StringPower { public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); String str = in.readString(); int[] heavy = new int[200000]; int[] metal = new int[200000]; int h = 0, m = 0; int countHeavy = 0, countMetal = 0; for (int i = 0; i < str.length() - 4;) { if ("heavy".equals(str.substring(i, i + 5))) { heavy[h++] = i; i = i+5; countHeavy++; } else if ("metal".equals(str.substring(i, i + 5))) { metal[m++] = i; i = i+5; countMetal++; }else{ i++; } } long count = 0; m = 0; for(int i=0;i<countHeavy && m<countMetal;){ if(heavy[i] > metal[m]){ m++; }else{ count += countMetal - m; i++; } } out.print(count); out.close(); } 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 (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String readLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.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
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; long long f = 0, p = 0; for (int i = 0; i < a.length(); i++) { string s = ""; if (i + 5 <= a.length()) s += a[i], s += a[i + 1], s += a[i + 2], s += a[i + 3], s += a[i + 4]; if (s == "heavy") p++; else if (s == "metal") f += p; } cout << f; 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 = str(input()) n = len(s) hcount = 0 count=0 for i in range(n-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': hcount+=1 if s[i] == 'm' and s[i+1] == 'e' and s[i+2] == 't' and s[i+3] == 'a' and s[i+4] == 'l': count = count + hcount 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
s=raw_input() ans=0 a=s.split('heavy') for i,b in enumerate(a): 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
s=raw_input() t=s.split("metal") S=0 for i,j in enumerate(t): a=j.count("heavy") S+=a*(len(t)-1-i) print S
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 = raw_input() count = 0 ans = 0 for i in range(len(s)): if s[i:i + 5] == 'heavy': count += 1 elif s[i:i + 5] == 'metal': ans += count 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
//package codeforces; import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class B implements Closeable { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer stringTokenizer; B() throws IOException { // reader = new BufferedReader(new FileReader("cycle2.in")); // writer = new PrintWriter(new FileWriter("cycle2.out")); } String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } final int MOD = 1000 * 1000 * 1000 + 9; int sum(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } int product(int a, int b) { return (int) (1l * a * b % MOD); } @SuppressWarnings("unchecked") void solve() throws IOException { String heavy = "heavy"; String metal = "metal"; String s = next(); int hc = 0; long answer = 0; for(int i = 0; i + metal.length() <= s.length(); i++) { if(heavy.equals(s.substring(i, i + 5))) { hc++; } else { if(metal.equals(s.substring(i, i + 5))) { answer += hc; } } } writer.println(answer); } public static void main(String[] args) throws IOException { try (B b = new B()) { b.solve(); } } @Override public void close() throws IOException { reader.close(); writer.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
s, total, heavy = input(), 0, [] for i in range(len(s)-4): if s[i : i + 5] == 'heavy' : heavy.append(i) if s[i : i + 5] == 'metal' : total += len(heavy) print(total)
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 javafx.beans.NamedArg; import java.io.*; import java.net.Inet4Address; import java.util.Comparator; import java.util.*; /** * @author Yuxuan Wu */ public class Main { public static final int RANGE = 1000000; static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; static int MODULO = 1000000007; static Scanner sc = new Scanner(System.in); static int multi = 0; private static void solve() { String s = sc.next(); char[] str = s.toCharArray(); ArrayList<Pairs<Integer, Integer>> dict = new ArrayList<>(); int len = str.length; String[] a = s.split("heavy"); int times = 0; long ans = 0; for (int i = 0; i < a.length; i++) { String[] cur = (a[i] + " ").split("metal"); int add = cur.length - 1; ans += times * (Math.max(add, 0)); times++; } System.out.println(ans); } public static void main(String[] args) { if(multi == 0){ solve(); } else { int T = sc.nextInt(); for(int t = 1; t <= T; ++t){ solve(); } } } /** * 返回最大公约数 * 时间复杂度 O(Log min(n1, n2)) */ public static long gcd(long n1, long n2) { if (n2 == 0) { return n1; } return gcd(n2, n1 % n2); } /** * 返回该数的位数 */ public static int getDigit(long num){ int ans = 0; while(num > 0){ num /= 10; ans++; } return ans; } /** * 判断是否为回文串 */ public static boolean palindrome(String s) { int n = s.length(); for(int i = 0; i < n; i++) { if(s.charAt(i) != s.charAt(n - i - 1)) { return false; } } return true; } /** * 判断是否为完全平方数 */ public static boolean isSquare(int num) { double a = 0; try { a = Math.sqrt(num); } catch (Exception e) { return false; } int b = (int) a; return a - b == 0; } public static class Pairs<K, V> implements Comparable<Pairs> { private K key; private V value; public K getKey() { return key; } public V getValue() { return value; } public void setKey(K key) { this.key = key; } public void setValue(V value) { this.value = value; } public Pairs(@NamedArg("key") K key, @NamedArg("value") V value) { this.key = key; this.value = value; } @Override public String toString() { return key + " = " + value; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pairs<?, ?> pairs = (Pairs<?, ?>) o; return Objects.equals(key, pairs.key) && Objects.equals(value, pairs.value); } @Override public int hashCode() { return Objects.hash(key, value); } @Override public int compareTo(Pairs o) { return 0; } } static class CompInt implements Comparator<Pairs>{ @Override public int compare(Pairs o1, Pairs o2) { if(o1.key instanceof Integer&&o2.key instanceof Integer){ if((int)o1.key>(int)o2.key){ return 1; }else{ return -1; } } return 0; } } static class CompLong implements Comparator<Pairs>{ @Override public int compare(Pairs o1, Pairs o2) { return Long.compare((long)o1.key, (long)o2.key); } } static class CompDouble implements Comparator<Pairs>{ @Override public int compare(Pairs o1, Pairs o2) { if(o1.key instanceof Double && o2.key instanceof Double){ return Double.compare((double) o1.key, (double) o2.key); } return 0; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) { return; } din.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
import java.util.*; import java.io.*; public class prob{ public static void main(String args[]) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); char[] a=st.nextToken().toCharArray(); int currentHeavy=0; int l=a.length; int i=0; long ans=0; while(i<l){ char c=a[i]; if(c=='h'){ if(i<(l-4) && a[i+1]=='e' && a[i+2]=='a' && a[i+3]=='v' && a[i+4]=='y'){ currentHeavy++; i+=4; } else i++; } else if(c=='m'){ if(i<(l-4) && a[i+1]=='e' && a[i+2]=='t' && a[i+3]=='a' && a[i+4]=='l'){ ans+=currentHeavy; i+=4; } else i++; } else{ i++; } } 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
#include <bits/stdc++.h> using namespace std; int main() { char arr[1000001]; cin >> arr; long long int len = strlen(arr); long long int cnt = 0; long long int inc = 0; long long int m = 4; for (long long int i = m; i < len; i++) { if (arr[i - 4] == 'h' && arr[i - 3] == 'e' && arr[i - 2] == 'a' && arr[i - 1] == 'v' && arr[i] == 'y') { inc++; i = i + 4; } if (arr[i - 4] == 'm' && arr[i - 3] == 'e' && arr[i - 2] == 't' && arr[i - 1] == 'a' && arr[i] == 'l') { cnt += inc; i = i + 4; } } cout << cnt << 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 = list(input()) ans, cnt = 0, 0 for i in range(1, len(s) - 3): if s[-i] == 'l' and s[-i - 1] == 'a' and s[-i - 2] == 't' and s[-i - 3] == 'e' and s[-i - 4] == 'm': cnt += 1 elif s[-i] == 'y' and s[-i - 1] == 'v' and s[-i - 2] == 'a' and s[-i - 3] == 'e' and s[-i - 4] == 'h': ans += cnt 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; const long long INF = 1e9; const long long N = 1e6 + 1; const long long mod = 1e9 + 7; const long double eps = 1E-7; long long n; string s; long long x, a[N]; int main() { ios_base::sync_with_stdio(0); cin >> s; n = s.size(); for (int i = n - 5; i >= 0; i--) { long long p = 0; p += s[i] == 'm'; p += s[i + 1] == 'e'; p += s[i + 2] == 't'; p += s[i + 3] == 'a'; p += s[i + 4] == 'l'; a[i] = a[i + 1] + p / 5; } for (int i = 0; i < n - 5; i++) { long long p = 0; p += s[i] == 'h'; p += s[i + 1] == 'e'; p += s[i + 2] == 'a'; p += s[i + 3] == 'v'; p += s[i + 4] == 'y'; x = x + p / 5 * a[i]; } cout << x << 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 java.util.*; import static java.lang.Math.*; import java.io.*; public class SolutionB { public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); TreeMap<Integer, Long> tree = new TreeMap<Integer, Long>(); List<Integer> met = new ArrayList<Integer>(); String h = "heavy"; String m = "metal"; long cnt = 0; for(int i = 0; i <= s.length() - 5; i++){ boolean found = true; for(int j = 0; j < 5; j++){ if(s.charAt(i+j) != h.charAt(j)){ found = false; break; } } if(found){ tree.put(i+4, ++cnt); } found = true; for(int j = 0; j < 5; j++){ if(s.charAt(i+j) != m.charAt(j)){ found = false; break; } } if(found) met.add(i); } long ans = 0; for(int i : met){ Integer key = tree.floorKey(i); if(key != null) ans += tree.get(key); } 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
#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); string s1 = "heavy"; string s2 = "metal"; int n, m, i, k, l, j, c1 = 0, f; long long int ans = 0; n = strlen(s); for (i = 0; i < n; i++) { if (s[i] == 'h') { j = i; f = 0; k = 0; while (j < i + 5) { if (s[j++] != s1[k++]) { f = 1; break; } } if (f == 0) c1++; } if (c1 != 0) { if (s[i] == 'm') { f = 0; j = i; k = 0; while (j < i + 5) { if (s[j++] != s2[k++]) { f = 1; break; } } if (f == 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
s = input() heavy = 0 total = 0 i = 0 while i < len(s) - 4: if s[i:i + 5] == 'heavy': i += 5 heavy += 1 elif s[i:i + 5] == 'metal': i += 5 total += heavy else: i += 1 print("%d" % total)
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().split("metal") l = len(s)-1 found = 0 head = 'heavy' for i in range(l): mt = s[i].count(head) found += mt*(l-i) print(found)
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 reader = new BufferedReader(new InputStreamReader(System.in)); String line = reader.readLine(); int len = line.length(); line = line.replace("heavy", "*"); int lenH = line.length(); int countH = (len - lenH) / 4; len = lenH; line = line.replace("metal", "#"); int lenM = line.length(); int countM = (len - lenM) / 4; if (countH == 0 || countM == 0) { System.out.println(0); return; } len = lenM; int cM,cH; long ans =0; cM = cH = 0; for(int i=0;i<len;i++) { if(line.charAt(i)=='*') cH++; else if(line.charAt(i)=='#') { cM++; ans += cH; if(cM==0) break; } } 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
line = raw_input() tmp = line result = 0 n = line.count("heavy") m = line.count("metal") arr = [] for i in range(len(line)): if line[i:i+5] == "heavy": result += m elif line[i:i+5] == "metal": m = m-1 print result
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() { long long ans = 0, m = 0, h = 0; vector<char> v; string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (i < s.size() - 4 && s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { i += 4; v.push_back('h'); } else if (i < s.size() - 4 && s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { v.push_back('m'); i += 4; m++; } } for (int i = 0; i < v.size(); i++) { if (v[i] == 'm') m--; if (v[i] == 'h') ans += m; } 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; template <typename T> string toString(T x) { if (x == 0) return "0"; bool negative = x < 0; string res; while (x) { res.push_back('0' + x % 10); x /= 10; } if (negative) res.push_back('-'); reverse(res.begin(), res.end()); return res; } template <typename T> T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } int main() { string s; cin >> s; int a = 0; long long res = 0; int till = s.length(); till -= 4; for (int i = 0; i < till; i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') a++; if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') res += a; } cout << res; 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
def f(a): return a*(a+1)//2 x=input() if "heavy" in x: x=x.replace("heavy","@") if "metal" in x: x=x.replace("metal","#") c=0 n=len(x) res=0 for i in range(n): if x[i]=="@": c+=1 if x[i]=="#": res+=c 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
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long i = 0, a = 0, b = 0; while (i < s.length()) { if (s.substr(i, 5) == "heavy") { a++; i += 5; } else if (s.substr(i, 5) == "metal") { b += a; i += 5; } else ++i; } cout << b; 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() total = 0 heavy = [] for i in range(len(s)-4): if s[i:i+5] == 'heavy': heavy.append(i) if s[i:i+5] == 'metal': total += len(heavy) print(total)
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 aprail15 { static class InputReader { private InputStream stream; private byte[] inbuf = new byte[1024]; private int start= 0; private int end = 0; public InputReader(InputStream stream) { this.stream = stream; } private int readByte() { if (start == -1) throw new UnknownError(); if (end >= start) { end= 0; try { start= stream.read(inbuf); } catch (IOException e) { throw new UnknownError(); } if (start<= 0) return -1; } return inbuf[end++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } public static void main(String args[]) { InputReader sc=new InputReader(System.in); char c[]=sc.next().toCharArray(); int c1[]=new int[c.length]; int l=c.length; List<Integer> list=new ArrayList<>(); for(int i=0;i<c.length-4;i++) { if(c[i]=='h' && c[i+1]=='e' && c[i+2]=='a' && c[i+3]=='v' && c[i+4]=='y') { list.add(i); c1[i]=0; } else if(c[i]=='m' && c[i+1]=='e' && c[i+2]=='t' && c[i+3]=='a' && c[i+4]=='l' ) c1[i]=1; } long co=0,sum=0; for(int i=1;i<l;i++) { c1[i]+=c1[i-1]; } int d=c1[l-1]; //list.forEach(System.out::print); //System.out.println(); //System.out.println(Arrays.toString(c1)+"\n"+d); for(int i=0;i<list.size();i++) { sum+=d-c1[list.get(i)]; } 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; string s; long long cnt[1000007], n, res = 0; int main() { cin >> s; n = (long long)s.size(); cnt[0] = 0; for (long long i = 0; i <= (n - 5); ++i) { if (i > 0) cnt[i] = cnt[i - 1]; string t = s.substr(i, 5); if (t == "heavy") ++cnt[i]; } for (long long i = n - 5; i >= 0; --i) { string t = s.substr(i, 5); if (t == "metal") res += cnt[i - 1]; } cout << res; }
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() h = 0 ans = 0 i = 0 for i in range(len(s)): if s[i] == "h" and s[i:i+5] == "heavy": h += 1 if s[i] == "m" and s[i:i+5] == "metal": if h >= 1: ans += h 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() { char c[1000000]; long long int heavy_count = 0, result = 0; gets(c); int i = 0; while (i < strlen(c)) { if (c[i] == 'h') { i++; if (c[i] == 'e') { i++; if (c[i] == 'a') { i++; if (c[i] == 'v') { i++; if (c[i] == 'y') { i++; heavy_count++; } } } } } else if (c[i] == 'm') { i++; if (c[i] == 'e') { i++; if (c[i] == 't') { i++; if (c[i] == 'a') { i++; if (c[i] == 'l') { i++; result += heavy_count; } } } } } else i++; } cout << result; 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.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringsOfPower implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { String s = in.next(); long result = 0L, heavy = 0; for (int i = 0; i <= s.length() - 5; i++) { String temp = s.substring(i, i + 5); if ("heavy".equals(temp)) { heavy++; } else if ("metal".equals(temp)) { result += heavy; } } out.println(result); } @Override public void close() throws IOException { in.close(); out.close(); } static 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(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (StringsOfPower instance = new StringsOfPower()) { instance.solve(); } } }
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.TreeSet; import java.util.*; public class StringsOfPower { BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } void solve() throws IOException { String s = nextToken(); List<Integer> l1 = new ArrayList<Integer>(); int pos = 0; while (pos < s.length() && (pos = s.indexOf("heavy", pos)) != -1) { l1.add(pos); pos+=5; } List<Integer> l2 = new ArrayList<Integer>(); pos = 0; while (pos < s.length() && (pos = s.indexOf("metal", pos)) != -1) { l2.add(pos); pos+=5; } long tot = 0; int m = 0; for (int i : l1) { while (m < l2.size() && l2.get(m) < i) m++; tot += l2.size() - m; } writer.println(tot); } static public void main(String[] args) { new StringsOfPower().run(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, s, f; cin >> n >> m >> s >> f; map<long long int, pair<long long int, long long int> > hash; int i; long long int mx = INT_MIN; for (i = 0; i < m; i++) { long long int x, y, z; cin >> x >> y >> z; hash[x] = {y, z}; } int step = 1; while (s != f) { if (hash.find(step) != hash.end()) { long long int l = hash[step].first; long long int r = hash[step].second; if (s >= l && s <= r) { cout << "X"; } else { if (f > s && !(s + 1 >= l && s + 1 <= r)) { cout << "R"; s++; } else if (f < s && !(s - 1 >= l && s - 1 <= r)) { cout << "L"; s--; } else { cout << "X"; } } } else { if (f > s) { cout << "R"; s++; } else if (f < s) { cout << "L"; s--; } } step++; } cout << "\n"; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
import java.io.*; import java.util.*; import java.*; import java.math.BigInteger; public class zad { private static BufferedReader in; private static StringTokenizer tok; private static PrintWriter out; private static String readToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private static int readInt() throws IOException { return Integer.parseInt(readToken()); } private static double readDouble() throws IOException { return Double.parseDouble(readToken()); } private static long readLong() throws IOException { return Long.parseLong(readToken()); } static int partition (int[] array, int start, int end) { int marker = start; for ( int i = start; i <= end; i++ ) { if ( array[i] <= array[end] ) { int temp = array[marker]; array[marker] = array[i]; array[i] = temp; marker += 1; } } return marker - 1; } static void quicksort (int[] array, int start, int end) { int pivot; if ( start >= end ) { return; } pivot = partition (array, start, end); quicksort (array, start, pivot-1); quicksort (array, pivot+1, end); } public static void Solve() throws IOException{ int n=readInt(); int m = readInt(); int s = readInt(); int f = readInt(); char x ='m'; int p = 0; if(s>f) { p=-1; x='L'; } else{ p=1; x='R'; } StringBuilder st= new StringBuilder(); int en =0; int time =0; boolean e = false; for(int i =0;i<m;i++){ time++; int t=readInt(); int l= readInt(); int r = readInt(); if(e) continue; for(;time<t;time++){ s+=p; st.append(x); if(s==f){ en= time; e = true; break; } } time =t; if (e) continue; int need=s+p; if (need>=l && need<=r || s>=l && s <=r){ st.append('X'); }else{ st.append(x); s=need; if(s==f){ en= time; e = true; break; } } } while(s!=f){ st.append(x); s+=p; } out.println(st); } public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Solve(); in.close(); out.close(); } }
JAVA
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
R=lambda:map(int,raw_input().split()) n,m,s,f=R() t=[R() for _ in range(m)]+[[10**9+9,0,0]] a,d=('R',1) if s<f else ('L',-1) i,j,v=1,0,'' while s!=f: b=s+d while t[j][0]<i:j+=1 if t[j][0]==i and ((s>=t[j][1] and s<=t[j][2]) or (b>=t[j][1] and b<=t[j][2])): v+='X' else: v+=a s=b #print i,s,b,f,t[j],v i+=1 print v
PYTHON
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
#include <bits/stdc++.h> using namespace std; int dir; int n, m, s, f; int tcurr = 1; string ret; int getNext() { if (dir == 0) return s + 1; else return s - 1; } char getNextMove() { if (dir == 0) return 'R'; else return 'L'; } int main() { cin >> n >> m >> s >> f; ret = ""; if (s <= f) dir = 0; else dir = 1; for (int i = 0; i < m; ++i) { int t, a, b; cin >> t >> a >> b; while (tcurr < t && s != f) { s = getNext(); ret += getNextMove(); ++tcurr; } if (s == f) break; if (((a <= s && s <= b)) || (a <= getNext() && getNext() <= b)) ret += 'X'; else { s = getNext(); ret += getNextMove(); } ++tcurr; if (s == f) break; } while (s != f) { s = getNext(); ret += getNextMove(); } cout << ret; return 0; }
CPP
342_B. Xenia and Spies
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). Input The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm. Output Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. Examples Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
2
8
def kansi(t,tim,x): if tim in t: z = t[tim] if z[0] <= x <= z[1]: return True else: return False else: return False def solver(): n,m,s,f = map(int, raw_input().split()) t = {} for _ in xrange(m): line = map(int,raw_input().split()) t[line[0]] = (line[1],line[2]) tim = 0 ans = "" now = s while now != f: tim += 1 if f - now > 0: if not(kansi(t,tim,now+1)) and not(kansi(t,tim,now)): ans += 'R' now += 1 else: ans += 'X' else: if not(kansi(t,tim,now-1)) and not(kansi(t,tim,now)): ans += 'L' now -= 1 else: ans += 'X' print ans if __name__ == "__main__": solver()
PYTHON