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
#include <bits/stdc++.h> using namespace std; const int NMax = 1001000; char A[NMax]; int N; long long sum[NMax]; vector<int> S; int main() { scanf("%s", A); N = strlen(A); for (int i = 4; i < N; i++) { if (A[i - 4] == 'h' && A[i - 3] == 'e' && A[i - 2] == 'a' && A[i - 1] == 'v' && A[i - 0] == 'y') S.push_back(0); if (A[i - 4] == 'm' && A[i - 3] == 'e' && A[i - 2] == 't' && A[i - 1] == 'a' && A[i - 0] == 'l') S.push_back(1); } for (int i = S.size() - 1; i >= 0; i--) sum[i] = sum[i + 1] + S[i]; long long ret = 0; for (int i = 0; i < S.size(); i++) if (S[i] == 0) ret += sum[i + 1]; printf("%I64d\n", ret); getchar(); getchar(); 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()[::-1] h = "heavy"[::-1] m = "metal"[::-1] sum = 0 cnt_b = 0 if h and m in s: for i in range(len(s)): if s[i: i + len(m)] == m: cnt_b += 1 elif s[i: i + len(h)] == h: sum += cnt_b print(sum)
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=str(input()) s=s[::-1] count=0 ans=0 for i in range(0,len(s)): if s[i:i+5]=="latem": count+=1 if s[i:i+5]=="yvaeh": ans+=count 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
n = cou = 0 t = input().split('heavy') for i in t: cou += i.count('metal')*n n+=1 print(cou)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Locale; public class array2 { public static void main(String[] args) { InputStreamReader in=new InputStreamReader(System.in); BufferedReader con=new BufferedReader(in); try { String s=con.readLine(); int heavy=0; long res=0; for (int i = 0; i <= s.length()-5; i++) { String temp=s.substring(i, i+5); if(temp.equals("heavy")) heavy++; else if(temp.equals("metal")) res+=heavy; } System.out.println(res); } catch (Exception e) { e.printStackTrace(); } } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s=input() h=0 m=0 ans=0 for i in range(len(s)-1,3,-1): # print(s[i],end='') #print(s[i-4:i+1]) if s[i-4:i+1]=='heavy': h+=1 ans+=m if s[i-4:i+1]=='metal': m+=1 print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
str = raw_input() ans = 0 heavy = 0 for i in xrange(len(str)): if str[i:i+5] == "heavy": heavy += 1 if str[i:i+5] == "metal": ans += heavy print ans
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; char str[2100000]; const char id1[] = "heavy"; const char id2[] = "metal"; int main() { scanf("%s", str); int len = strlen(str); long long cntofh = 0; long long cntofm = 0; long long ans = 0; int i, j; bool judge; for (i = 0; i < len; i++) { if (str[i] == 'h') { judge = true; for (j = 0; j < 5 && i + j < len; j++) { if (str[i + j] != id1[j]) { judge = false; break; } } if (j != 5) judge = false; if (judge) cntofh++; } else if (str[i] == 'm') { judge = true; for (j = 0; j < 5 && i + j < len; j++) { if (str[i + j] != id2[j]) { judge = false; break; } } if (j != 5) judge = false; if (judge) { cntofm++; ans += (cntofh); } } } cout << ans << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner s=new Scanner(System.in); String temp=s.nextLine(); int he[]=new int[1000000]; //int me[]=new int[100000]; int k=0; int l=0,m=0; int nt=temp.indexOf("heavy",k); while(nt>=0) { he[l++]=nt; k=nt+1; nt=temp.indexOf("heavy",k); } k=0; nt=temp.indexOf("metal",k); long ans=0; while(nt>=0) { //System.out.println(nt); // for(int i=0;i<l;i++) // { // if(he[i]<nt)ans++; // else break; // } long sk=-Arrays.binarySearch(he,0,l,nt)-1; //System.out.println(sk); ans+=sk; k=nt+1; nt=temp.indexOf("metal",k); } 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 H[1000002], M[1000002]; int l, ll; int main() { string str; cin >> str; long long int cnt = 0; for (int i = 0; i < str.size(); i++) if (str[i] == 'h' && str.substr(i, 5) == "heavy") H[l++] = i; else if (str[i] == 'm' && str.substr(i, 5) == "metal") M[ll++] = i; for (int i = 0, j = 0; i < l;) if (H[i] < M[j]) { cnt += ll - j; i++; } else { if (ll != j) j++; else i++; } 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
#include <bits/stdc++.h> using namespace std; int main() { char a[1000100]; scanf("%s", a); int len = strlen(a) - 4; long long ans = 0, h = 0, i, j, k; for (i = 0; i < len; i++) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') h++, i += 4; else if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') ans += h, i += 4; } printf("%lld\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
#include <bits/stdc++.h> using namespace std; int main() { string s; long long h, m, ans; cin >> s; int n = s.length(); string s1, s2, r1, r2; s1 = "heavy"; s2 = "metal"; r1 = "heavy"; r2 = "metal"; h = 0; m = 0; ans = 0; for (int i = 0; i < n; i++) { if ((s[i] == 'h') && (i + 5 <= n)) { for (int j = 0; j < 5; j++) { r1[j] = s[i + j]; } if (r1 == s1) h++; } if ((s[i] == 'm') && (i + 5 <= n)) { for (int j = 0; j < 5; j++) { r2[j] = s[i + j]; } if (r2 == s2) { ans += h; } } } cout << ans << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import io,os # speedforce input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #deactivate when input contains string from collections import deque as que, defaultdict as vector from bisect import bisect as bsearch from heapq import* inin = lambda: int(input()) inar = lambda: list(map(int,input().split())) inst= lambda: input().decode().rstrip('\n\r') INF=float('inf') _T_=1 #inin() for _t_ in range(_T_): s=inst() hv=0 ans=0 for i in range(len(s)-4): if s[i:i+5]=='heavy': hv+=1 if s[i:i+5]=='metal': ans+=hv print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.Scanner; public class StringsOfPower { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); in.close(); int currentIndex = 0; int nextHeavy = s.indexOf("heavy", currentIndex); int nextMetal = s.indexOf("metal", currentIndex); int numHeavy = 0; long sol = 0; while (nextMetal != -1) { if (nextHeavy != -1 && nextHeavy < nextMetal) { numHeavy++; currentIndex = nextHeavy + 5; nextHeavy = s.indexOf("heavy", currentIndex); } else if (nextHeavy == -1 || nextMetal < nextHeavy) { sol += numHeavy; currentIndex = nextMetal + 5; nextMetal = s.indexOf("metal", currentIndex); } } System.out.println(sol); } }
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() h = 0 ans = 0 for i in range(len(s)): c = s[i : i + 5] if c == "metal": ans += h if c == "heavy": h += 1 print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); char[] arr = str.toCharArray(); long h =0; long m =0; for(int i=0;i<str.length()-4;i++){ if(arr[i]=='h'&& arr[i+1]=='e' && arr[i+2]=='a' && arr[i+3]=='v' && arr[i+4]=='y')h++; if(arr[i]=='m'&& arr[i+1]=='e' && arr[i+2]=='t' && arr[i+3]=='a' && arr[i+4]=='l')m+=h; } System.out.println(m); } }
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.*; import java.util.*; public class B { public static void solution(BufferedReader reader, PrintWriter writer) throws IOException { In in = new In(reader); Out out = new Out(writer); String s = in.next(); FenwickTree h = new FenwickTree(s.length() + 1); for (Integer i : find(s, "heavy")) h.update(i, 1); long total = 0; for (Integer m : find(s, "metal")) total += h.cumulativeSum(m); out.println(total); } private static ArrayList<Integer> find(String s, String w) { ArrayList<Integer> a = new ArrayList<Integer>(); int i = 0; while (true) { int j = s.indexOf(w, i); if (j != -1) { a.add(j + 1); i = j + w.length(); } else break; } return a; } protected static class FenwickTree { private int[] tree; // tree[0] is never used public FenwickTree(int n) { tree = new int[n + 1]; } public int cumulativeSum(int i) { int sum = 0; for (; i > 0; i -= i & (-i)) sum += tree[i]; return sum; } public void update(int i, int value) { for (; i < tree.length; i += i & (-i)) tree[i] += value; } } public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out))); solution(reader, writer); writer.close(); } protected static class In { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public In(BufferedReader reader) { this.reader = reader; } public String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] nextIntArray1(int n) throws IOException { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } public int[] nextIntArraySorted(int n) throws IOException { int[] a = nextIntArray(n); Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); int t = a[i]; a[i] = a[j]; a[j] = t; } Arrays.sort(a); return a; } public long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public long[] nextLongArray1(int n) throws IOException { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } public long[] nextLongArraySorted(int n) throws IOException { long[] a = nextLongArray(n); Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); long t = a[i]; a[i] = a[j]; a[j] = t; } Arrays.sort(a); return a; } } protected static class Out { private PrintWriter writer; public Out(PrintWriter writer) { this.writer = writer; } public void print(char c) { writer.print(c); } public void print(int a) { writer.print(a); } public void println(String s) { writer.println(s); } public void println(int a) { writer.println(a); } public void println(long a) { writer.println(a); } public void println(Object[] os) { for (int i = 0; i < os.length; i++) { writer.print(os[i]); writer.print(' '); } writer.println(); } public void println(int[] a) { for (int i = 0; i < a.length; i++) { writer.print(a[i]); writer.print(' '); } writer.println(); } public void println(long[] a) { for (int i = 0; i < a.length; i++) { writer.print(a[i]); writer.print(' '); } writer.println(); } } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s=input() i=0 j=len(s)-1 c='metal' p='heavy' h=[] m=[] while i<len(s)-4 and j>=4: if s[i:i+5]==p: h.append(i) i+=1 if s[j-4:j+1]==c: m.append(j) j-=1 c=0 for i in range(len(m)): lower=0 upper=len(h)-1 while lower<=upper: mid=(lower+upper)//2 if h[mid]>m[i]: upper=mid-1 elif h[mid]<m[i]: lower=mid+1 elif h[mid]==m[i]: break c+=lower 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
from bisect import bisect_left s=input() heavy=[] driver=[] for i in range(len(s)): if i+5<=len(s): if s[i:i+5]=="heavy": heavy.append(i) if i+5<=len(s): if s[i:i+5]=="metal": driver.append(i) ans=0 for j in heavy: x=bisect_left(driver,j) ans+=len(driver)-x print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import math import time str = input() array = [] for i in range(4, len(str)): if str[i-4] == 'h' and str[i-3] == 'e' and str[i-2] == 'a' and str[i-1] == 'v' and str[i]=='y': array.append(1) if str[i-4] == 'm' and str[i-3] == 'e' and str[i-2] == 't' and str[i-1] == 'a' and str[i]=='l': array.append(0) pos = [0 for i in range(len(array))] tot = 0 for i in range(len(pos)): j = len(pos)-1-i pos[j] = tot if array[j] == 0: tot+=1 res = 0 for i in range(len(pos)): if array[i]==1: res+=pos[i] print(res)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
str = input() heavy = 0 ans = 0 for i in range(0, len(str)): if str[i:i + 5] == "heavy": heavy += 1 elif str[i:i + 5] == "metal": ans += heavy print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s=input() count,ans,i=0,0,0 while(i<len(s)-4): if(s[i:i+5]=="heavy"): count+=1 i+=5 elif(s[i:i+5]=="metal"): ans+=count i+=5 else: i+=1 print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.*; public class CF188B { public static long heaveMetal(String s) { long ans = 0; long heavy = 0; for(int i = 0; i <= s.length()-5; i++) { String part = s.substring(i, i+5); if(part.equals("heavy")) heavy++; if(part.equals("metal")) ans += heavy; } return ans; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); System.out.println(heaveMetal(s)); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; string s; int main() { long long m = 0, res = 0; cin >> s; for (int i = s.length() - 5; i >= 0; i--) { if (s.substr(i, 5) == "metal") m++; else if (s.substr(i, 5) == "heavy") 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
arr=input() countex=0 countin=0 for i in range(len(arr)): if(arr[i:i+5]=="heavy"): countin+=1 if(arr[i:i+5]=="metal"): countex+=countin print(countex)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
r=h=0 for k in raw_input().split("heavy"): r+=k.count("metal")*h h+=1 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
import java.io.*; public class Main{ int[] array; public Main(int size) { array = new int[size + 1]; } public int rsq(int ind) { assert ind > 0; int sum = 0; while (ind > 0) { sum += array[ind]; ind -= ind & (-ind); } return sum; } public int rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, int value) { assert ind > 0; while (ind < array.length) { array[ind] += value; ind += ind & (-ind); } } public int size() { return array.length - 1; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); Main sp = new Main(line.length()); int ind; int aux = 0; while ((ind = line.indexOf("heavy", aux)) != -1) { sp.update(ind + 1, 1); aux = ind + 1; } long sum = 0; aux = 0; while ((ind = line.indexOf("metal", aux)) != -1) { sum += sp.rsq(ind); aux = ind + 1; } 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
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Stack; public class Haha{ public static void main(String[] args) throws IOException{ BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); PrintWriter ww = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); Stack ss = new Stack(); String str = s.readLine().replaceAll("heavy", "[").replaceAll("metal","]"); int len = str.length(); long res=0; for(int i=0;i<len;i++){ if(str.charAt(i) == '[') ss.push('['); else if(str.charAt(i) == ']') res += ss.size(); } ww.println(res); ww.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 sys inS = raw_input() size = len(inS) inS += '*'*10 h = 0 out = 0 for i in range(size): if inS[i] == 'h': if inS[i:i+5] == "heavy": h += 1 elif inS[i] == 'm': if inS[i:i+5] == "metal": out += h sys.stdout.write(str(out))
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.*; public class yvr{ public static void main(String args[]){ Scanner s=new Scanner(System.in); String a=s.nextLine(); String x="heavy"; String y="metal"; long c=0,p=0; for(int i=a.length()-5;i>=0;i--){ if(a.substring(i,i+5).equals(y)==true){ c++; } else if(a.substring(i,i+5).equals(x)==true){ p=p+c; } } System.out.println(p); } }
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=raw_input() m=0 ans=0 for i in xrange(len(s)-4): temp=s[i:i+5] if temp=="heavy": m+=1 if temp=="metal": ans+=m 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
st=input() ch=0 c=0 cm=0 for i in range(0,len(st)-4): if(st[i:i+5]=="heavy"): ch+=1 elif(st[i:i+5]=="metal"): if cm==0 and ch>=1: cm=1 cm=cm*ch else: cm=cm+ch print(cm)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys, re inp = sys.stdin.read().strip() m = re.compile('(heavy|metal)').findall(inp) res = 0 if m: n = [0 for i in m] s = 0 for i in xrange(len(m)-1, -1, -1): if m[i] == 'metal': s += 1 n[i] = s for i in xrange(len(m)): if m[i] == 'heavy': res += n[i] sys.stdout.write(str(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
import java.util.*; import java.io.*; public class a { public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); String s = input.next(); int n = s.length(); ArrayList<Integer> heavy = new ArrayList<Integer>(), metal = new ArrayList<Integer>(); for(int i = 0; i<n-4; i++) if(s.charAt(i) == 'h' && s.charAt(i+1) == 'e' && s.charAt(i+2) == 'a' && s.charAt(i+3) == 'v' && s.charAt(i+4) == 'y') heavy.add(i); else if(s.charAt(i) == 'm' && s.charAt(i+1) == 'e' && s.charAt(i+2) == 't' && s.charAt(i+3) == 'a' && s.charAt(i+4) == 'l') metal.add(i); int i = 0, j = 0; long res = 0; while(i<heavy.size() && j<metal.size()) { while(j<metal.size() && metal.get(j) < heavy.get(i)) j++; if(j == metal.size()) break; res += (metal.size() - j); i++; } out.println(res); out.close(); } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } } static class IT { int[] left,right, val, a, b; IT(int n) { left = new int[4*n]; right = new int[4*n]; val = new int[4*n]; a = new int[4*n]; b = new int[4*n]; init(0,0, n); } int init(int at, int l, int r) { a[at] = l; b[at] = r; if(l==r) left[at] = right [at] = -1; else { int mid = (l+r)/2; left[at] = init(2*at+1,l,mid); right[at] = init(2*at+2,mid+1,r); } return at++; } //return the sum over [x,y] int get(int x, int y) { return go(x,y, 0); } int go(int x,int y, int at) { if(at==-1) return 0; if(x <= a[at] && y>= b[at]) return val[at]; if(y<a[at] || x>b[at]) return 0; return go(x, y, left[at]) + go(x, y, right[at]); } //add v to elements x through y void add(int x, int y, int v) { go3(x, y, v, 0); } void go3(int x, int y, int v, int at) { if(at==-1) return; if(y < a[at] || x > b[at]) return; val[at] += (Math.min(b[at], y) - Math.max(a[at], x) + 1)*v; go3(x, y, v, left[at]); go3(x, y, v, right[at]); } } }
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.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class codeforces { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static boolean set[]; public static void main(String args[])throws IOException { String X=in.next(); int l=X.length(); X+=" "; int c=0; int A[]=new int[l+8]; long s=0; for(int i=l; i>=5; i--) { String b=X.substring(i-5,i); //System.out.println(b);; if(b.equals("metal")) { for(int j=i; j>i-5; j--)A[j]=c; c++; i-=4; } A[i-1]=c; } //for(int i:A)System.out.print(i+" "); c=0; for(int i=0; i<l; i++) { String a=X.substring(i,i+5); if(a.equals("heavy")) { s+=A[i+5]; } } System.out.println(s); } static void setGraph(int N) { set=new boolean[N+1]; g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) g.add(new ArrayList<Integer>()); } static void DFS(int N,int d) { set[N]=true; d++; for(int i=0; i<g.get(N).size(); i++) { int c=g.get(N).get(i); if(set[c]==false) { DFS(c,d); } } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; const long long INF64 = (long long)1e18; long double eps = 1e-6; const long double pi = 3.14159265358979323846; const int N = 1e6 + 100; long long fact[N], modulo = INF + 7; int h[N] = {0}; int m[N] = {0}; int main() { string a; cin >> a; int l = a.length(); for (int i = 0; i < l; i++) { if (i + 4 < l) if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') { h[i] = 1; } } for (int i = 0; i < l; i++) { if (i + 4 < l) if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') { m[i] = 1; } } for (int i = l - 2; i >= 0; i--) { m[i] = m[i + 1] + m[i]; } long long count = 0; for (int i = 0; i < l; i++) { if (h[i] == 1) { count += m[i]; } } cout << count; }
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 INF = (0u - 1) / 2; char S[1000002]; int main() { long long n = 0; gets(S); int i = 0, j = 0; vector<int> v1, v2; char A[10] = "heavy!"; char B[10] = "metal!"; while (S[i] != 0) { if (S[i] == A[j]) { ++j; if (A[j] == '!') { j = 0; v1.push_back(i); } } else { j = 0; if (S[i] == A[j]) j++; } ++i; } i = 0; j = 0; while (S[i] != 0) { if (S[i] == B[j]) { ++j; if (B[j] == '!') { j = 0; v2.push_back(i); } } else { j = 0; if (S[i] == B[j]) j++; } ++i; } j = 0; for (int i = 0; i < v1.size(); ++i) { while (j < v2.size()) { if (v2[j] < v1[i]) j++; else break; } n += (long long)v2.size() - j; } printf("%I64d\n", n); return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## import math import bisect mod = 998244353 # for _ in range(int(input())): from collections import Counter # sys.setrecursionlimit(10**6) # dp=[[-1 for i in range(n+5)]for j in range(cap+5)] # arr= list(map(int, input().split())) # n,l= map(int, input().split()) # arr= list(map(int, input().split())) # for _ in range(int(input())): # n=int(input()) # for _ in range(int(input())): import bisect from heapq import * from collections import defaultdict,deque def okay(x,y): if x<0 or x>=3 : return False if y<n and mat[x][y]!=".": return False if y+1<n and mat[x][y+1]!=".": return False if y+2<n and mat[x][y+2]!=".": return False return True '''for i in range(int(input())): n,m=map(int, input().split()) g=[[] for i in range(n+m)] for i in range(n): s=input() for j,x in enumerate(s): if x=="#": g[i].append(n+j) g[n+j].append(i) q=deque([0]) dis=[10**9]*(n+m) dis[0]=0 while q: node=q.popleft() for i in g[node]: if dis[i]>dis[node]+1: dis[i]=dis[node]+1 q.append(i) print(-1 if dis[n-1]==10**9 else dis[n-1])''' '''from collections import deque t = int(input()) for _ in range(t): q = deque([]) flag=False n,k = map(int, input().split()) mat = [input() for i in range(3)] vis=[[0 for i in range(105)]for j in range(3)] for i in range(3): if mat[i][0]=="s": q.append((i,0)) while q: x,y=q.popleft() if y+1>=n: flag=True break if vis[x][y]==1: continue vis[x][y]=1 if (y+1<n and mat[x][y+1]=='.' and okay(x-1,y+1)==True): q.append((x-1,y+3)) if (y+1<n and mat[x][y+1]=='.' and okay(x,y+1)==True): q.append((x,y+3)) if (y+1<n and mat[x][y+1]=='.' and okay(x+1,y+1)==True): q.append((x+1,y+3)) if flag: print("YES") else: print("NO") # ls=list(map(int, input().split())) # d=defaultdict(list)''' from collections import defaultdict #for _ in range(int(input())): #n=int(input()) #n,k= map(int, input().split()) #arr=sorted([i,j for i,j in enumerate(input().split())]) s=input() h=0 m=0 n=len(s) i=0 ans=0 while i<n: if s[i]=="h": if i+4<n: if s[i:i+5]=="heavy": i+=5 h+=1 else: i+=1 else: break elif s[i]=="m": if i+4<n: if s[i:i+5]=="metal": ans+=h i += 5 else: i+=1 else: break else: i+=1 print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
r, z=0, 0 for i in input().split('heavy'): r+=z*i.count('metal') 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
counter=0 ans=0 for i in raw_input().split('heavy'): ans+=i.count('metal')*counter counter+=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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { 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) { FastReader sc=new FastReader(); long s_heavy=0; long sum=0; String s=sc.next(); for(int i=0;i<=s.length()-5;i++) { String ss1=s.substring(i,i+5); if(ss1.equals("heavy")) { s_heavy++; } else if(ss1.equals("metal")) { sum=sum+s_heavy; } } 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; char a[1000010]; int main() { scanf("%s", a); int len = strlen(a) - 4; long long int ans = 0, h = 0, i, j, k; for (i = 0; i < len; i++) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') { i += 4; h++; } else if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') { i += 4; ans += h; } } printf("%lld\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
#include <bits/stdc++.h> using namespace std; const int MAXN = 1001000; char str[MAXN]; char h[] = "heavy"; char m[] = "metal"; int main() { scanf("%s", str); int left = 0; int a = 0; int b = 0; long long ans = 0; for (int i = 0; str[i]; ++i) { if (str[i] != h[a]) a = 0; if (str[i] == h[a]) ++a; if (str[i] != m[b]) b = 0; if (str[i] == m[b]) ++b; if (b == 5) { ans += left; } if (a == 5) { ++left; } } 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
res = 0 i = 0 for word in input().split('heavy'): res += i * word.count('metal') i += 1 print(res)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.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; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { String s = in.readString(); String HEAVEY = "heavy"; String METAL = "metal"; long count = 0; int hc = 0; int hIndex = 0; while (hIndex < s.length()) { int temp = hIndex; // System.out.println(s.charAt(hIndex)); if (temp + 5 <= s.length() && s.charAt(temp++) == 'h' && s.charAt(temp++) == 'e' && s.charAt(temp++) == 'a' && s.charAt(temp++) == 'v' && s.charAt(temp++) == 'y') { hc++; hIndex = temp; continue; } temp = hIndex; if (temp + 5 <= s.length() && s.charAt(temp++) == 'm' && s.charAt(temp++) == 'e' && s.charAt(temp++) == 't' && s.charAt(temp++) == 'a' && s.charAt(temp++) == 'l') { count += hc; hIndex = temp; continue; } hIndex++; } out.printLine(count); // while (true) { // int hi = s.indexOf(HEAVEY, hIndex); // int mi = s.indexOf(METAL, hIndex); // if (mi == -1) // break; // if (hi != -1) { // if (hi < mi) { // hIndex = hi + HEAVEY.length(); // hc++; // continue; // } else { // count = count + hc; // hIndex = mi + METAL.length(); // continue; // } // // } // // count = count + hc; // hIndex = mi + METAL.length(); // } // out.printLine(count); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; 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 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 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 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } 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 print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) writer.print(' '); writer.print(array[i]); } } public void printLine(int[] array) { print(array); writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } class IOUtils { public static List<Integer> readIntList(InputReader in, int size) { List<Integer> list = new LinkedList<Integer>(); for (int i = 0; i < size; i++) list.add(in.readInt()); return list; } public static int[] readIntArray(InputReader in, int size) { int a[] = new int[size]; for (int i = 0; i < size; i++) a[i] = in.readInt(); return a; } }
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 = raw_input() count = 0 ans = 0 for i in range(len(s)): if s[i:i + 5] == 'heavy': count += 1 i += 5 elif s[i:i + 5] == 'metal': ans += count i += 5 print(ans)
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.*; import java.lang.*; import java.io.*; public class Temp { static int imax = Integer.MAX_VALUE, imin = Integer.MIN_VALUE; static long lmax = Long.MAX_VALUE, lmin = Long.MIN_VALUE; static long mod = (long) 1e9 + 7; public static void main(String[] args) throws java.lang.Exception { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int test = 1; int i = 0, j = 0; while (test-- > 0) { // int n = in.ni(); String str = in.ns(); StringBuilder bdr = new StringBuilder("heavy$"); String str1 = bdr.append(str).toString(); int lcp[] = lcp_zalgorithm(str1.toCharArray()); int arr1[] = new int[str.length() + 6]; int arr2[] = new int[str.length() + 6]; int cnt=0; for(i=6;i<str.length() + 6;i++){ if(lcp[i]==5){ cnt++; } arr1[i]=cnt; } // System.out.println(Arrays.toString(lcp)); // System.out.println(Arrays.toString(arr1)); bdr = new StringBuilder("metal$"); str1 = bdr.append(str).toString(); int lcp2[] = lcp_zalgorithm(str1.toCharArray()); long ans=0; for(i=6;i<str.length() + 6;i++){ if(lcp2[i]==5){ ans+=arr1[i]; } } // System.out.println(Arrays.toString(lcp2)); System.out.println(ans); // System.out.println(max); // out.println(Arrays.toString(lcpKMP(arr))); } out.close(); } static int[] lcp_zalgorithm(char str[]) { int len = str.length; int lcp[] = new int[len]; int i = 0, j = 1, l = 1, r = 1; while (true) { if (r < len && str[i] == str[r]) { r++; i++; } else { if (i == 0) { l++; r++; } else { // System.out.println(l+" "+r+" "+i); lcp[l] = i; l++; j = 1; while (l < r && lcp[j] + l < r) { lcp[l] = lcp[j]; j++; l++; } i = r - l; } if (r == len && l == len) break; } } return lcp; } static int[] calculateZ(char input[]) { int Z[] = new int[input.length]; int left = 0; int right = 0; for (int k = 1; k < input.length; k++) { if (k > right) { left = right = k; while (right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } else { // we are operating inside box int k1 = k - left; // if value does not stretches till right bound then just copy // it. if (Z[k1] < right - k + 1) { Z[k] = Z[k1]; } else { // otherwise try to see if there are more matches. left = k; while (right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } } } return Z; } static int[] lcpKMP(char str[]) { int len = str.length; int lcp[] = new int[str.length]; int i = 1, j = 0; while (i < len) { if (str[i] == str[j]) { lcp[i] = j + 1; i++; j++; } else { if (j == 0) { i++; } else { j = lcp[j - 1]; } } } return lcp; } static class Pair { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } } static void print(int arr[], int len) { for (int i = 0; i < len; i++) System.out.print(arr[i] + " "); System.out.println(); } 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 int ni() { 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 & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { 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 & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String ns() { 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 char[] ncs() { return ns().toCharArray(); } public String nLine() { int c = read(); // while (c != '\n' && c != '\r' && c != '\t' && c != -1) // c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (c != '\n' && c != '\r' && c != '\t' && c != -1); return res.toString(); } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; char str[1000005]; int sum[1000005]; int main(void) { long long ans; int n, i; scanf("%s", str + 1); n = strlen(str + 1); for (i = n - 4; i >= 1; i--) { sum[i] = sum[i + 1]; if (str[i] == 'm' && str[i + 1] == 'e' && str[i + 2] == 't' && str[i + 3] == 'a' && str[i + 4] == 'l') sum[i]++; } ans = 0; for (i = 5; i <= n; i++) { if (str[i] == 'y' && str[i - 1] == 'v' && str[i - 2] == 'a' && str[i - 3] == 'e' && str[i - 4] == 'h') ans += sum[i + 1]; } printf("%lld\n", ans); return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.*; import java.util.*; public class Main{ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String nexts() throws IOException { tokenizer = new StringTokenizer(reader.readLine()); String s=""; while (tokenizer.hasMoreTokens()) { s+=tokenizer.nextElement()+" "; } return s; } //String str=nextToken(); //String[] s = str.split("\\s+"); public static int gcd(int x, int y){ if (y == 0) return x; else return gcd(y, x % y); } public static boolean isPrime(int n) { // Corner cases if (n <= 1){ return false; } if (n <= 3){ return true; } // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0){ return false; } for (int i = 5; i * i <= n; i = i + 6) { //Checking 6i+1 & 6i-1 if (n % i == 0 || n % (i + 2) == 0) { return false; } } //O(sqrt(n)) return true; } public static void shuffle(int[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } public static long power(int x, long n) { long mod = 1000000007; if (n == 0) { return 1; } long pow = power(x, n / 2); if ((n & 1) == 1) { return (x * pow * pow)%mod; } return (pow * pow)%mod; } static int ncr(int n, int k) { // Base Cases if (k == 0 || k == n) return 1; // Recur return ncr(n - 1, k - 1) + ncr(n - 1, k); } public static int lower_bound(int[] A, int lo, int hi, int x) { if (A[lo] >= x) { return lo; } else if (A[hi] < x) { return hi + 1; } while (lo <= hi) { int mid = (lo + hi) / 2; if (A[mid] >= x) { hi = mid - 1; } else { lo = mid + 1; } } return hi + 1; } static class R implements Comparable<R>{ int x, y; public R(int x, int y) { this.x = x; this.y = y; } public int compareTo(R o) { return x-o.x; //Increasing order(Which is usually required) } } // int t=a[i]; // a[i]=a[j]; // a[j]=t; //double d=Math.sqrt(Math.pow(Math.abs(x2-x1),2)+Math.pow(Math.abs(y2-y1),2)); public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } private static void solve() throws IOException { // int t = nextInt(); // while(t-->0){ //long n = nextLong(); String h="heavy"; String m="metal"; String s= nextToken(); //long[] a=new long[n]; //ArrayList<Integer> ar=new ArrayList<Integer>(); //HashSet<Integer> set=new HashSet<Integer>(); //HashMap<Integer,String> h=new HashMap<Integer,String>(); //R[] a1=new R[n]; //char[] c=nextToken().toCharArray(); int n=s.length(); long[] a=new long[n]; for(int i=0;i<n-4;i++){ int c=0; for(int j=0;j<5;j++){ if(s.charAt(i+j)!=h.charAt(j)){ c=1; } } if(c==0){ a[i]=1; } } for(int i=1;i<n;i++){ a[i]+=a[i-1]; } // for(int i=1;i<n;i++){ // writer.print(a[i]); // } long ans=0; for(int i=0;i<n-4;i++){ int c=0; for(int j=0;j<5;j++){ if(s.charAt(i+j)!=m.charAt(j)){ c=1; } } if(c==0){ // writer.print("99"); ans+=a[i]; } } writer.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.io.*; import java.util.*; public class cf_Strings_Of_Power { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { char[] arr = sc.next().toCharArray(); int heavy = 0; long ans = 0; int n = arr.length; for(int i = 0;i < n;i++) { if(arr[i] == 'h') { if(heavy(i, arr, n))heavy++; } else if(arr[i] == 'm') { if(metal(i, arr, n)) { ans += heavy; } } } System.out.println(ans); } public static boolean heavy(int i, char[] arr, int n) { return i + 4 < n && arr[i + 1] == 'e' && arr[i + 2] == 'a' && arr[i + 3] == 'v' && arr[i + 4] == 'y'; } public static boolean metal(int i, char[] arr, int n) { return i + 4 < n && arr[i + 1] == 'e' && arr[i + 2] == 't' && arr[i + 3] == 'a' && arr[i + 4] == 'l'; } }
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 inp, heavy{"heavy"}, metal{"metal"}; cin >> inp; long long check{}; for (size_t k{0}; k < inp.size(); k++) { if (inp.substr(k, 5) == metal) { check++; k = k + 4; } } long long count{}; for (size_t i{}; i < inp.size(); i++) { if (inp.substr(i, 5) == heavy) { count += check; i += 4; } if (inp.substr(i, 5) == metal) { check--; i += 4; } } cout << count; 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; bool chk(string &s, string &temp, int i) { string x = s.substr(i, 5); return (x == temp); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> ws >> s; int n = s.size(); vector<int> h, m(n); m[n - 1] = 0; string a = "heavy", b = "metal"; for (int i = 0; i < n; ++i) { if (chk(s, a, i)) { h.push_back(i); } } for (int i = n - 2; i >= 0; --i) { m[i] = m[i + 1]; if (chk(s, b, i)) { ++m[i]; } } long long sum = 0; for (int i : h) { sum += m[i + 1]; } cout << 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
a=list(input().split('metal')) ;z=0;l=len(a) for i in range(l): z+=(l-i-1)*a[i].count('heavy') print(z)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Map; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Soly { static final int INF = Integer.MAX_VALUE; static void mergeSort(int[] a, int p, int r) { if (p < r) { int q = (p + r) >> 1; mergeSort(a, p, q); mergeSort(a, q + 1, r); merge(a,p, q, r); } } static void merge(int[] a, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] L = new int[n1 + 1], R = new int[n2 + 1]; // int[] L1 = new int[n1 + 1], R1 = new int[n2 + 1]; for (int i = 0; i < n1; i++) { L[i] = a[p + i]; // L1[i] = b[p + i]; } for (int i = 0; i < n2; i++) { R[i] = a[q + 1 + i]; // R1[i] = b[q + 1 + i]; } L[n1] = R[n2] = INF; // L1[n1] = R1[n2] = INF; for (int k = p, i = 0, j = 0; k <= r; k++) { if (L[i] <= R[j]) { a[k] = L[i++]; // b[k] = L1[i++]; } else { a[k] = R[j++]; // b[k] = R1[j++]; } } } static int last(ArrayList<Integer>a,int v){ int s=0,e=a.size()-1,mid=0,ans=-1; while (s<=e) { mid=(e+s)>>1; if(a.get(mid)>v){ ans=mid; e=mid-1; } else s=mid+1; } return ans; } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); try (PrintWriter or = new PrintWriter(System.out)) { String s= in.next(); ArrayList<Integer> h = new ArrayList(); ArrayList<Integer> m = new ArrayList(); int size=s.length(); for (int i = 0; i+4 < size; ++i) { String f=s.substring(i,i+5); if(f.equals("heavy")) h.add(i); else if(f.equals("metal")) m.add(i); } long ans=0; size=m.size(); for(int i:h){ int y=last(m, i); if(y>=0)ans+=(size-y); } or.print(ans); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) { if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) { f *= 10; } } } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } } class Pair8 { int index; int value; // int min; public Pair8(int index, int value) { this.value = value; this.index = index; //min = m; } }
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 = raw_input().strip() Len = len(s) cnt = ans = 0 for i in xrange(Len): if s[i] == 'h' and i+5<=Len and s[i:i+5]=='heavy': cnt += 1; if s[i] == 'm' and i+5<=Len and s[i:i+5]=='metal': ans += cnt; print ans
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int k = 0, n, h = 0; string input; cin >> input; n = input.length(); for (int i = 0; i < n; i++) { if (input.substr(i, 5) == "heavy") h++; if (input.substr(i, 5) == "metal") k += h; } cout << k << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.*; import java.io.*; public class stringsOfPower { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String s = sc.next(); ArrayList<Character> al = new ArrayList<>(); for(int i = 0; i <= s.length() - 5; i++){ if(s.substring(i, i + 5).equals("heavy")) al.add('H'); else if(s.substring(i, i + 5).equals("metal")) al.add('M'); } long countM = 0; long ans = 0; for(int i = al.size() - 1; i >= 0; i--){ if(al.get(i) == 'H') ans += countM; else countM++; } 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() { string s; int i; cin >> s; vector<int> a1, a2; if (s.length() < 5) { cout << 0 << endl; return 0; } for (i = 0; i < s.length() - 4; i++) { if (s[i] == 'h' && s.substr(i, 5) == "heavy") { a1.push_back(i); } else if (s[i] == 'm' && s.substr(i, 5) == "metal") { a2.push_back(i); } } vector<int>::iterator it; long long int ans = 0; for (i = 0; i < a1.size(); i++) { int d = a1[i]; it = lower_bound(a2.begin(), a2.end(), d); if (it != a2.end()) { int d1 = (a2.end() - it); ans += (long long int)(d1); } } cout << ans << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String word = scanner.next(); int heavy = 0; long number = 0; for (int i = 0; i <= word.length() - 5; i++) { String temp = word.substring(i, i + 5); if (temp.equals("heavy")) heavy++; else if (temp.equals("metal")) number += heavy; } System.out.println(number); } }
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.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Sanchit M. Bhatnagar ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { String line = in.nextLine().trim(); ArrayList<Integer> heavyList = new ArrayList<Integer>(); int last = 0; while ((last = line.indexOf("heavy", last)) != -1) { heavyList.add(last); last++; } ArrayList<Integer> metalList = new ArrayList<Integer>(); last = 0; while ((last = line.indexOf("metal", last)) != -1) { metalList.add(last); last++; } long count = 0; int idx = 0; for (int i=0; i<heavyList.size(); i++) { while (idx < metalList.size() && metalList.get(idx) < heavyList.get(i)){ idx++; } count += metalList.size() - idx; } 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
str=raw_input() l=len(str) num=0 val=0 for i in range (l-4): if str[i]=='h' and str[i+1]=='e' and str[i+2]=='a' and str[i+3]=='v' and str[i+4]=='y': num+=1 if str[i]=='m' and str[i+1]=='e' and str[i+2]=='t' and str[i+3]=='a' and str[i+4]=='l': val+=num print val
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; vector<int> a; string temp1 = "heavy"; string temp2 = "metal"; int pos = 0; int i = 0; while (i < s.length()) { if (s[i] == 'h') { pos = 0; while (pos < 5 && i < s.length() && s[i] == temp1[pos]) { ++i; ++pos; } if (pos == 5) a.push_back(1); } else if (s[i] == 'm') { pos = 0; while (pos < 5 && i < s.length() && s[i] == temp2[pos]) { ++i; ++pos; } if (pos == 5) a.push_back(-1); } else { ++i; } } unsigned long long rez = 0; int k_pos = 0; for (int i = 0; i < a.size(); ++i) { if (a[i] == 1) ++k_pos; if (a[i] == -1) rez += k_pos; } cout << rez; 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() res = 0 h = 0 for i in range(len(s)): if s[i:i+5] == 'heavy': h += 1 elif s[i:i+5] == 'metal': res += h 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
s=input() def heavy(s,i): if i-4>=0 and s[i]=='y' and s[i-1]=='v' and s[i-2]=='a' and s[i-3]=='e' and s[i-4]=='h': return 1 return 0 def metal(s,i): if i-4>=0 and s[i]=='l' and s[i-1]=='a' and s[i-2]=='t' and s[i-3]=='e' and s[i-4]=='m': return 1 return 0 a=0 count=0 i=len(s)-1 while i>0: if metal(s,i): a=a+1 i=i-5 elif heavy(s,i): count=count+a i=i-5 else: i=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
import java.util.*; public class Main{ public static void main(String[] args) { Scanner s=new Scanner(System.in); // String str=s.next(); Queue<Integer> heavy =new LinkedList<>(); Queue<Integer> metal =new LinkedList<>(); //heavy metals checking int i,j; i=0; while(str.indexOf("heavy",i)!=-1){ i=str.indexOf("heavy",i); heavy.offer(i); i=i+5; } i=0; while(str.indexOf("metal",i)!=-1){ i=str.indexOf("metal",i); metal.offer(i); i=i+5; } int n=heavy.size(); int m=metal.size(); i=0; j=0; int temp=0; long ans=0; while(metal.size()>0 && heavy.size()>0){ temp=heavy.poll(); while(metal.size()>0 && metal.peek()<temp){ metal.poll(); } ans += metal.size(); } System.out.print(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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class StringsOfPower { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); int heavy = 0; long answer = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == 'h') { String heavyStr = "heavy"; int j; boolean flag = true; for(j = i + 1; j < s.length() && (j - i) < heavyStr.length() && (flag = s.charAt(j) == heavyStr.charAt(j - i)); j++); if (flag && j <= s.length() && (j - i) == heavyStr.length()) { heavy++; i = j - 1; } } else if (c == 'm'){ String heavyStr = "metal"; int j; boolean flag = true; for(j = i + 1; j < s.length() && (j - i) < heavyStr.length() && (flag = s.charAt(j) == heavyStr.charAt(j - i)); j++); if (flag && j <= s.length() && (j - i) == heavyStr.length()) { answer += heavy; i = j - 1; } } } System.out.println(answer); } }
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.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception { Scanner sc = null; PrintWriter pr = null; pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); // sc = new Scanner(new File("input.txt")); String str = sc.next(); String str1 = "heavy"; String str2 = "metal"; int n1 = 0; int n2 = 0; long res = 0; int i = 0; while (i < str.length()) { if (str.charAt(i) == 'h' && (i + 4) < str.length()) { int j = 0; for (j = 0; j < 5; j++) { if (str.charAt(i + j) != str1.charAt(j)) break; } if (j == 5) { n1++; } if (j == 5) i += 4; else i += j; } else if (str.charAt(i) == 'm' && (i + 4) < str.length()) { int j = 0; for (j = 0; j < 5; j++) { if (str.charAt(i + j) != str2.charAt(j)) break; } if (j == 5) { n2++; res += n1; } if (j == 5) i += 4; else i += j; } else i++; } pr.println(res); pr.close(); sc.close(); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); 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 nextInt() { return Integer.parseInt(next()); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s = input() c = 0 answer = 0 s = "".join(reversed(s)) for i in range(len(s) - 4): if s[i:i+5] == "latem": c+=1 elif s[i:i+5] == 'yvaeh': answer += c print(answer)
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.*; public class Div2_318B { public static void main(String[] args) { @SuppressWarnings("resource") Scanner in = new Scanner(System.in); String str = in.next(); List<Integer> h = new ArrayList<Integer>(), m = new ArrayList<Integer>(); long cnt = 0; int i = str.indexOf("heavy"); while (i != -1) { h.add(i); i = str.indexOf("heavy", i + 1); } i = str.indexOf("metal"); while (i != -1) { m.add(i); i = str.indexOf("metal", i + 1); } for (int j = 0; j < h.size(); j++) { // for (int k = 0; k < m.size(); k++) { // if (m.get(k) > h.get(j)){ // cnt += m.size() - k; // break; // } // } int idx = Math.abs(Collections.binarySearch(m, h.get(j))); cnt += m.size() - idx + 1; } System.out.println(cnt); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys import math # # Turn of Garbage collection. For speed up in program. # import gc; # gc.disable(); inp = raw_input() a = inp.split('heavy') ans = 0 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
from sys import stdin def main(): cadena =stdin.readline().strip() i = 0 j = i+4 heavy = 0 ans = 0 while j< len(cadena): if cadena[i:j+1] == "heavy": heavy +=1 elif cadena[i:j+1] == "metal": ans += heavy i+=1 j+=1 print(ans) 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
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word = sc.nextLine(); long heavy=0; long total=0; for (int i = 0; i < word.length() - 4; i++) { if (word.substring(i, i + 5).equals("heavy")) { heavy++; }else if(word.substring(i, i + 5).equals("metal")) { total=total+heavy; } } System.out.println(total); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
from collections import * from math import * from sys import * s = input() n = len(s) ct = 0 ans = 0 for i in range(n-4): if(s[i:i+5] == "heavy"): ct += 1 elif(s[i:i+5] == "metal"): ans += ct 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; string str; int main() { cin >> str; vector<int> heavy, metal; for (int i = 0; i < max((int)(str.size() - 4), 0); i++) { string str1 = str.substr(i, 5); if (str1 == "heavy") heavy.push_back(i); if (str1 == "metal") metal.push_back(i); } long long a = 0; for (int i = 0, j = 0; i < heavy.size() && j < metal.size();) { if (heavy[i] < metal[j]) { a += metal.size() - j; i++; } else j++; } cout << a; 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; const long long N = 1e6 + 1; string g; deque<long long> v, vc; long long cnt; void DNM() { cin >> g; if (g.size() < 5) { cout << 0; return; } for (long long i = 0; i < g.size() - 4; i++) { if (g[i] == 'h' and g[i + 1] == 'e' and g[i + 2] == 'a' and g[i + 3] == 'v' and g[i + 4] == 'y') v.push_back(i); else if (g[i] == 'm' and g[i + 1] == 'e' and g[i + 2] == 't' and g[i + 3] == 'a' and g[i + 4] == 'l') vc.push_back(i); } while (v.size() > 0 and vc.size() > 0) { if (vc[0] > v[0]) { cnt += vc.size(); v.erase(v.begin()); } else vc.erase(vc.begin()); } cout << cnt << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int Qu_l_uQ = 1; while (Qu_l_uQ--) DNM(); }
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
song, heavy_count, result = input(), 0, 0 for i in range(len(song) - 4): if song[i:i+5] == 'heavy': heavy_count += 1 elif song[i:i+5] == 'metal': result += heavy_count print(result)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2, s3; int cnt1 = 0, cnt2 = 0; long long res = 0; cin >> s1; s1 += " "; for (int i = 0; i < s1.size() - 8; i++) { if (s1.substr(i, 5) == "heavy") cnt1++; if (s1.substr(i, 5) == "metal") res += cnt1; } 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() heavycount = 0 ans = 0 for i in range(len(s)-4): if s[i:i+5]=='heavy': heavycount+=1 elif s[i:i+5]=='metal': ans+=heavycount 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; char s[1000005], *k; int p, q; long long cnt; vector<int> v; int main() { scanf("%s", s); while (NULL != (k = strstr(s + p, "heavy"))) { p = k - s; v.push_back(p); p += 1; } while (NULL != (k = strstr(s + q, "metal"))) { q = k - s; cnt += lower_bound(v.begin(), v.end(), q) - v.begin(); q += 1; } 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
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long ans = 0; long long sum = 0; for (int i = 0; i < s.size(); i++) { string q = s.substr(i, 5); if (q == "heavy") { ans++; } if (q == "metal") { sum += ans; } } cout << 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
/** * * @author sarthak */ import java.util.*; import java.math.*; import java.io.*; public class rnd188_B { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { st = null; try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); return ""; } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } static class P implements Comparable{ private int x, y; public P(int x, int y) { this.x = x; this.y = y; } public int hashCode() { return (x * 31) ^ y; } public boolean equals(Object o) { if (o instanceof P) { P other = (P) o; return (x == other.x && y == other.y); } return false; } public int compareTo(Object obj) { P l = (P) obj; if (this.x == l.x){ if (this.y == l.y) return 0; return (this.y < l.y)? -1: 1; } return (this.x < l.x)? -1: 1; } } public static void main(String[] args){ FastScanner s = new FastScanner(System.in); StringBuilder op=new StringBuilder(); String ip=s.next();; ArrayList<Integer> hs=new ArrayList<>(); ArrayList<Integer> ms=new ArrayList<>(); int len=ip.length(); for(int i=0;i<len;i++){ if(ip.charAt(i)=='h'&&i+4<len&&ip.substring(i, i+5).equals("heavy")) hs.add(i); } for(int i=0;i<len;i++){ if(ip.charAt(i)=='m'&&i+4<len&&ip.substring(i, i+5).equals("metal")) ms.add(i); } int i=0; int j=0; long an=0; while(j<ms.size()&&i<hs.size()){ while(j<ms.size()&&hs.get(i)>ms.get(j)) j++; if(j>=ms.size()) break; an+=(long)ms.size()-(long)j; i++; } System.out.println(an); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys s = sys.stdin.readline()[:-1] heavy = [] metal = [] for i in range(len(s)-4): #print(s[i]) w = s[i:i+5] #print(w) if w == "heavy" : heavy.append(i) elif w == "metal": metal.append(i) #print(word) #print(index) c = 0 x = 0 for i in range(len(heavy)): for j in range(x, len(metal)): if metal[j] > heavy[i]: c += (len(metal) - j) x = j break sys.stdout.write(str(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
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:268435456") int main() { string st; cin >> st; if (st.size() <= 5) { cout << 0; return 0; } long h = 0; string s; long long r = 0; long long d = 0; for (long i = 0; i <= st.size() - 5; i++) { s = st.substr(i, 5); if (s == "heavy") d++; if (s == "metal") r += d; } cout << r; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; long long l, sol, z, c, HEAVY[1000099], METAL[1000099]; char S[1000099]; int main() { scanf("%s", S); l = strlen(S); for (int i = 0; i < l; i++) { if (S[i] == 'h' && S[i + 1] == 'e' && S[i + 2] == 'a' && S[i + 3] == 'v' && S[i + 4] == 'y') { z++; } if (S[i] == 'm' && S[i + 1] == 'e' && S[i + 2] == 't' && S[i + 3] == 'a' && S[i + 4] == 'l') { sol += z; } } printf("%I64d", sol); 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
x = input() a = "heavy" b = "metal" c = 0 s = 0 for i in range(len(x)-4): if x[i:i+5] == a: c += 1 if x[i:i+5] == b: s += c print(s)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s=input() l=[] l1=[] j=-3 i=-3 while(True): j=s.find("metal",j+3) if j==-1 : break l.append(j) while(True): i=s.find("heavy",i+3) if i==-1 : break l1.append(i) p=len(l1) k1=0 j1=0 ost=0 out=0 for i in range(len(l)) : if l[i]>ost : for j in range(j1,p) : if l1[j]>l[i] : break j1+=1 ost=l1[j] k1+=1 out+=k1 print(out)
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 bisect s = input() answer = 0 hpos = [] metpos = [] for i in range(len(s)-4): if s[i:i+5] == "heavy": hpos.append(i+4) for i in range(len(s)-4): if s[i:i+5] == "metal": metpos.append(i) hpos.sort() metpos.sort() s = len(metpos) for pos in hpos: k = bisect.bisect_left(metpos, pos) if k != -1: answer += s-k print(answer)
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 n = s.length(); long long ans = 0, heavy = 0; for (long long i = 0; i < n - 4; i++) { if (s.substr(i, 5) == "heavy") heavy++; if (s.substr(i, 5) == "metal") ans += heavy; } 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; long long m, ans; string ss, s; long long i; int main() { cin >> s; for (i = 0; i + 4 < s.length(); i++) { ss = s.substr(i, 5); if (ss == "heavy") { m++; } if (ss == "metal") { 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
import java.io.*; import java.util.*; public class Main { StreamTokenizer in; BufferedReader inb; PrintWriter out; String problemname = "success"; public static void main(String[] args) throws Exception { new Main().run(); } public void run() throws Exception { inb = new BufferedReader( // new FileReader(problemname + ".in")); new InputStreamReader(System.in)); in = new StreamTokenizer(inb); out = new PrintWriter( // new FileWriter(problemname + ".out")); new OutputStreamWriter(System.out)); solve(); out.flush(); } int nextInt() throws Exception { in.nextToken(); return (int) in.nval; } double nextDouble() throws Exception { in.nextToken(); return in.nval; } String next() throws Exception { in.nextToken(); return in.sval; } public void solve() throws Exception { String s = next(); int n=s.length(); long heavy,ans; heavy=ans=0; for (int i=5;i<=n;i++){ if (s.substring(i-5, i).equals("heavy")) heavy++; if (s.substring(i-5, i).equals("metal")) ans+=heavy; } 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
s = raw_input() n = len(s) c = [0] * n i = s.find("heavy") while i != -1: c[i] += 1 i = s.find("heavy", i + 1) for i in xrange(n - 1): c[i + 1] += c[i] res = 0 i = s.find("metal") while i != -1: res += c[i] i = s.find("metal", i + 1) 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * */ /** * @author mohanad * */ public class Div2_318B { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String s=bf.readLine(); int ln=s.length()-4,heavyCnt=0; long ans=0; for (int i = 0 ; i<ln ; ++i){ String str=s.substring(i, i+5); if (str.equals("heavy")) ++heavyCnt; else if (str.equals("metal")) ans+=heavyCnt; } 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
z = res = 0 t = input().split('heavy') for i in t: res += i.count('metal')*z; z+=1 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
line = raw_input() count = 0 for i, h in enumerate(line.split("heavy")): count += i * h.count("metal") 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
#!/usr/bin/python import re #list2 = [[0 for x in xrange(25)] for x in xrange(25)] #list1 = [[0 for x in xrange(25)] for x in xrange(25)] aString = raw_input() #word="heavy" #if word in test: # print('success') #print test.find('heavy') #print test.rfind('heavy') #print test.find_all('heavy') #list1= [(a.start(), a.end()) for a in list(re.finditer('heavy', aString))] list1= [(a.start())for a in list(re.finditer('heavy', aString))] #print list1 list2= [(a.start()) for a in list(re.finditer('metal', aString))] #print list2 list3= aString #print list3 h_count=0 t_count=0 #coo=0 #for i in range(0,len(list3)): #print i #if((list2[count])-(list1[count1]))>0: # for metal in enumerate(list2): # coo+=1 #else: # for i in range(0,len(list2)): # count+=1 # if ((list2[count])-(list1[count1]))>0: # for metal in enumerate(list2): # coo+=1 # count1+=1 #print coo #for i in range(0,len(list2)): # for j in range(0,len(list1)): # if (list2[i]-list1[j])>0 : # coo+=1 #print coo for i in range(0,len(list3)): if list3[i:i+5]=="heavy": h_count+=1 elif list3[i:i+5]=="metal": t_count+=h_count else: continue print t_count
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string actual; cin >> actual; long long int l = actual.length(); string h = "heavy"; string m = "metal"; long long int add = 0; long long int ans = 0; for (long long int i = 0; i < l; i++) { if (actual.substr(i, 5) == h) { add++; } if (actual.substr(i, 5) == m) { ans += add; } } 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
// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95 import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.*; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; import static java.lang.String.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // (new FileReader("input.in")); StringBuilder out = new StringBuilder(); StringTokenizer tk; String s = in.readLine(); List<Integer> num = new ArrayList<>(); String h = "heavy",m = "metal"; boolean f; for(int i=0; i<s.length(); i++) { if(s.charAt(i)=='h' && i+4<s.length()) { f = true; for(int j=0; j<=4; j++) if(s.charAt(i+j)!=h.charAt(j)) { f = false; break; } if(f) { i += 4; num.add(0); } } else if(s.charAt(i)=='m' && i+4<s.length()) { f = true; for(int j=0; j<=4; j++) if(s.charAt(i+j)!=m.charAt(j)) { f = false; break; } if(f) { i += 4; num.add(1); } } } long ans = 0,cnt = 0; for(int i=num.size()-1; i>=0; i--) { if(num.get(i)==1) cnt++; else ans += cnt; } 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
s = raw_input() heavy = [] metal = [] tl = 0 res = 0 re = 0 pos = 0 l = len(s) for x in range(l-4): if s[x:x+5] == "heavy": tl +=1 heavy.append(x) elif s[x:x+5] == "metal": metal.append(x) res = res+tl #print(heavy) #print(metal) 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
def powerful(s): h = "heavy" m = "metal" i = 0 j = 0 dp = "" ans = 0 countH = 0 for it in range(len(s)): if s[it] == h[i]: i+=1 if i == 5: countH+=1 #dp+="h" i = 0 else: if s[it] == h[0]: i = 1 else: i = 0 if s[it] == m[j]: j+=1 if j == 5: #dp+="m" ans+=countH j = 0 else: if s[it] == m[0]: j = 1 else: j = 0 return ans s = input() print(powerful(s))
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; int a[1100000]; int b[1100000]; void init() {} int main() { init(); string s; cin >> s; for (int i = 0; i < 1100000; i++) a[i] = 0; for (int i = 0; i < s.size(); i++) { if (s.size() - 1 - i > 0) if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') a[i] = 1; } int cur = 0; for (int i = s.size() - 1; i > -1; i--) { cur += a[i]; b[i] = cur; } long long ans = 0; for (int i = 0; i < s.size(); i++) { if (s.size() - 1 - i > 0) if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') ans += b[i]; } cout << ans; }
CPP