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
str=input() countM=0 countResult=0 for i in range(0,len(str)-4): if(str[i:i+5]=="heavy"): countM=countM+1; elif(str[i:i+5]=="metal"): countResult=countResult+countM; print(countResult)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; char s[maxn]; void get_input() { cin >> s; } void solve() { long long ans = 0; long long kom = 0; long long i = 0; while (s[i] != 0) { if (s[i] == 'h') { i++; if (s[i] == 'e') { i++; if (s[i] == 'a') { i++; if (s[i] == 'v') { i++; if (s[i] == 'y') { kom++; i++; } } } } } else if (s[i] == 'm') { i++; if (s[i] == 'e') { i++; if (s[i] == 't') { i++; if (s[i] == 'a') { i++; if (s[i] == 'l') { ans += kom; i++; } } } } } else i++; } cout << ans; } void output() {} int main() { ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; while (t--) { get_input(), solve(), output(); } 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() dic = {} l = len(s) i = diclen = sub = 0 while 1: if i<=l-5: if s[i:i+5]=='heavy': dic[i] = 0 diclen += 1 i += 5 elif s[i:i+5]=='metal': sub += diclen i += 5 else: i += 1 else: break print(sub)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
a=input() n=len(a) b=[] c=[] y=k=0 for i in range(n-4): if a[i]=='h' : if a[i:i+5]=="heavy": b.append(i) i+=4 k+=1 if a[i]=='m': if a[i:i+5]=="metal": c.append(i) i+=4 y+=k else: i+=1 print(y)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s = raw_input() i, h, m = 0, 'heavy', 'metal' ha, ma = [], [] while(i + 5 <= len(s)): if s[i:i + 5] == h: i += 5 ha.append(i) elif s[i:i + 5] == m: i += 5 ma.append(i) else: i += 1 r, n = 0, len(ma) for j in ha: lo, hi = 0, n while(lo < hi): mid = (hi + lo) / 2 if ma[mid] < j: lo = mid + 1 else: hi = mid r += n - lo 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
ans = 0 s = input() heavy = 0 i = 0 if not len(s) < 5: while i < len(s) - 4: if s[i] == 'h' and s[i + 1] == 'e' and s[i + 2] == 'a' and s[i + 3] == 'v' and s[i + 4] == 'y': heavy += 1 i += 5 elif s[i] == 'm' and s[i + 1] == 'e' and s[i + 2] == 't' and s[i + 3] == 'a' and s[i + 4] == 'l': ans += heavy i += 5 else: i += 1 print(ans)
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 c1 = 0, c2 = 0; for (long long i = 0; i < s.size(); i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { c1 += 1; } if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { c2 += c1; } } cout << c2; }
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() { ios_base::sync_with_stdio(false); string s; set<int> h, m; set<int>::iterator it, itlow, itt; cin >> s; int posh = s.find("heavy"), posm = s.find("metal"); while (posh != string::npos) { h.insert(posh); posh = s.find("heavy", posh + 1, 5); } while (posm != string::npos) { m.insert(posm); posm = s.find("metal", posm + 1, 5); } long long p = m.size(), cnt = 0, pos = 0; itlow = m.begin(); for (it = h.begin(); it != h.end(); it++) { for (itt = itlow; itt != m.end(); itt++) { if (*itt > *it) { cnt += (p - pos); break; } pos++; } itlow = itt; } cout << cnt << "\n"; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; vector<int> a, b; if (s.length() < 5) { cout << "0"; return 0; } for (int i = 0; i < s.length() - 4; i++) { if (s[i] == 'h') { if (s.substr(i, 5) == "heavy") a.push_back(i); } } for (int i = 0; i < s.length() - 4; i++) { if (s[i] == 'm') { if (s.substr(i, 5) == "metal") b.push_back(i); } } int ind = 0; long long int cnt = 0; for (int i = 0; i < a.size(); i++) { if (b.size() == 0) break; if (a[i] < b[ind]) { cnt += b.size() - ind; } else { while (b[ind] < a[i] && ind < b.size()) ind++; if (ind != b.size()) cnt += b.size() - ind; } } cout << cnt; 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
r=input() S=r.split('heavy') k=0 for i in range(len(S)) : p=S[i].count('metal') k=k+i*p print(k)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException,InterruptedException{ Scanner sc=new Scanner(System.in); char[] arr=sc.next().toCharArray(); long c=0,cm=0; ArrayList<Character> arrayList=new ArrayList<>(); for (int i = 0; i < arr.length-4; i++) { String m=arr[i]+""+arr[i+1]+""+arr[i+2]+""+arr[i+3]+""+arr[i+4]; if (m.equals("heavy")) { arrayList.add('h'); }else if (m.equals("metal")) { arrayList.add('m'); cm++; } } char[] arr2=new char[arrayList.size()]; int p=0; for (char k : arrayList) { arr2[p++]=k; } for (int i = 0; i < arr2.length; i++) { if(arr2[i]=='h') { c+=cm; }else { cm--; } } pw.println(c); pw.close(); } static HashSet<Long> primes; static PrintWriter pw=new PrintWriter(System.out); static long pow(int a,int b) { long r=1l; for (int i = 0; i < b; i++) { r*=a; } return r; } static void primes() { boolean[] freq=new boolean[1000000]; for (int i = 0; i < freq.length; i++) { freq[i]=true; } primes=new HashSet<>(); for (int i = 2; i <= 1e3; i++) { if(freq[i-1]) { for (int j = i*i; j <= 1e6; j+=i) { freq[j-1]=false; } } } for (int i = 2; i < freq.length; i++) { if(freq[i-1]) primes.add((long)(i)); } } static long gcd(long x,long y) { while (x!=y) { if(Math.max(x,y)/Math.min(x,y)==(double)(Math.max(x,y))/Math.min(x,y)) return Math.min(x,y); if(primes.contains(x)) { if(y/x==y/(double)x) return x; else return 1; }else if (primes.contains(y)) { if(x/y==x/(double)y) return y; else return 1; } if(x>y) x-=y; else y-=x; } return x; } static class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair)o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(pair other) { if(this.x==other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { return this.y - other.y; } else { return this.x - other.x; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public boolean hasNext() { // TODO Auto-generated method stub return false; } 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(); } } }
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 Maxn = 1000 * 1000 + 100; int dp[Maxn]; int main() { string s; long long int k = 0; cin >> s; for (int i = s.size() - 5; i >= 0; i--) { if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { dp[i]++; } dp[i] += dp[i + 1]; } for (int i = 0; i < s.size(); i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { k += dp[i]; } } cout << k; }
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.*; import java.math.*; /* javac Main.java java Main > a.txt */ public class Main { public static void process()throws IOException { String s=nn(); int len=s.length(); int l=0,h=len-5; long freq=0; long ans=0; if(!s.contains("heavy")||!s.contains("metal")) pn(0); else{ while(l<=h) { if(s.substring(l,l+5).equals("heavy")) {l+=5;freq++;} else if(s.substring(l,l+5).equals("metal") && freq!=0) {ans+=(freq);l+=5;} else l++; } pn(ans); } } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc=new AnotherReader(); boolean oj = true; //PrintWriter out=new PrintWriter("output"); //oj = System.getProperty("ONLINE_JUDGE") != null; //if(!oj) sc=new AnotherReader(100); //int T=ni(); while(T-->0) process(); long s = System.currentTimeMillis(); out.flush(); if(!oj) System.out.println(System.currentTimeMillis()-s+"ms"); System.out.close(); } static void sort(long arr[],int n) { shuffle(arr,n); Arrays.sort(arr); } static void shuffle(long arr[],int n) { Random r=new Random(); for(int i=0;i<n;i++) { long temp=arr[i]; int pos=i+r.nextInt(n-i); arr[i]=arr[pos]; arr[pos]=temp; } } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);System.out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static String nn()throws IOException{return sc.next();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static int[] iarr(int N)throws IOException{int[]ARR=new int[N];for(int i=0;i<N;i++){ARR[i]=ni();}return ARR;} static long[] larr(int N)throws IOException{long[]ARR=new long[N];for(int i=0;i<N;i++){ARR[i]=nl();}return ARR;} static boolean multipleTC=false; ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ 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
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s=in.next(); int heavy=0; long metal=0; int i; for(i=4;i<s.length();++i) { if(s.charAt(i)=='l') { if(s.charAt(i-4)=='m' && s.charAt(i-3)=='e' && s.charAt(i-2)=='t' && s.charAt(i-1)=='a') { metal +=heavy; } } else if(s.charAt(i-4)=='h' && s.charAt(i-3)=='e' && s.charAt(i-2)=='a' && s.charAt(i-1)=='v' && s.charAt(i)=='y') { heavy++; } } System.out.println(metal); } }
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 gcd(int a, int b) { if (b > a) return gcd(b, a); return b == 0 ? a : gcd(b, a % b); } int main() { char s[1000001]; scanf("%s", s); long long int i, count = 0, ans = 0; for (i = 0; i < strlen(s); i++) { if (strncmp(s + i, "heavy", 5) == 0) { count++; } if (strncmp(s + i, "metal", 5) == 0) { ans += count; } } 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
a = raw_input() count, end = 0, 0 for i in range(0, len(a)): if a[i:(i+5)] == 'heavy': count += 1 elif a[i:(i+5)] == 'metal': end += count print end
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 10, MAX = 2e5 + 1e4, MOD = 1e9 + 7; void OUT(long double o, int x) { cout << fixed << setprecision(x) << o; return; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; long long x = 0, z = 0; for (int i = 4; i < s.size(); i++) { string q = ""; for (int j = i - 4; j <= i; j++) q += s[j]; if (q == "heavy") z++; q = ""; for (int j = i; j < i + 5 && j < s.size(); j++) q += s[j]; long long y = 0; if (q == "metal") y = 1; x += z * y; } cout << x; 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 JavaApp { public static Scanner in = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { String s = in.next(); String d; long ans = 0, H = 0; for (int i = 0; i < s.length() - 4; i++){ d = s.substring(i, i+5); if (d.equals("heavy")) ++H; else if(d.equals("metal")) ans += H; } out.println(ans); out.flush(); } }
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() metalPositions = [] i = 0 while True: i = s.find("metal", i) if i == -1: break; metalPositions.append(i) i += 5 count = 0 beg = 0 i = 0 while True: i = s.find("heavy", i) if i == -1: break i += 5 for j in range(beg, len(metalPositions)): if metalPositions[j] >= i: count += len(metalPositions) - j beg = j break 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
h,v=0,0 a=b='z'*5 for c in raw_input(): a,b=a[1:]+c,b[1:]+c h+=a=='heavy' v+=(b=='metal')*h print v
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import re import sys import bisect def find_gt(a, x, l): i = bisect.bisect_right(a, x) if i != len(a): return i return l l = sys.stdin.readline() h = [m.start() for m in re.finditer("heavy", l)] m = [m.start() for m in re.finditer("metal", l)] count = 0 l = len(m) for x in h: count += l - find_gt(m, x, l) 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/env python import re def main( ): l = input( ) heavyCnt = 0 res = 0 for i in range( 4, len( l ) ): if l[ i - 4 : i + 1 ] == "heavy": heavyCnt += 1 if l[ i - 4 : i + 1 ] == "metal": res += heavyCnt print( res ) if __name__ == '__main__': main( )
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.*; import java.io.*; public class String_of_power { private static int next(ArrayList<Integer> arr, int target) { int start = 0, end = arr.size() - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to right side if target is // greater. if (arr.get(mid) <= target) { start = mid + 1; } // Move left side. else { ans = mid; end = mid - 1; } } return ans; } public static void process()throws IOException { String s = S(); String p1 = "heavy"; ArrayList<Integer> bi1 = new ArrayList<Integer>(); int fromIndex1 = 0; while ((fromIndex1 = s.indexOf(p1, fromIndex1)) != -1) { bi1.add(fromIndex1 + 1); fromIndex1++; } String p = "metal"; ArrayList<Integer> bi = new ArrayList<Integer>(); int fromIndex = 0; while ((fromIndex = s.indexOf(p, fromIndex)) != -1) { bi.add(fromIndex + 1); fromIndex++; } long sum=0; for(int i=0;i<bi1.size();i++) { int t=next(bi,bi1.get(i)); if(t!=-1) { sum=sum+(bi.size()-t); } else { sum+=0; } } pn(sum); } static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static void pn(Object o){out.println(o);out.flush();} static void p(Object o){out.print(o);out.flush();} static void pni(Object o){out.println(o);System.out.flush();} static int I() throws IOException{return sc.nextInt();} static long L() throws IOException{return sc.nextLong();} static double D() throws IOException{return sc.nextDouble();} static String S() throws IOException{return sc.next();} static char C() throws IOException{return sc.next().charAt(0);} static int[] Ai(int n) throws IOException{int[] arr = new int[n];for (int i = 0; i < n; i++)arr[i] = I();return arr;} static String[] As(int n) throws IOException{String s[] = new String[n];for (int i = 0; i < n; i++)s[i] = S();return s;} static long[] Al(int n) throws IOException {long[] arr = new long[n];for (int i = 0; i < n; i++)arr[i] = L();return arr;} // *--------------------------------------------------------------------------------------------------------------------------------*// static class AnotherReader {BufferedReader br;StringTokenizer st;AnotherReader() throws FileNotFoundException {br = new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a) throws FileNotFoundException {br = new BufferedReader(new FileReader("input.txt"));} String next() throws IOException{while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}}return st.nextToken();} int nextInt() throws IOException {return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble() throws IOException {return Double.parseDouble(next());} String nextLine() throws IOException {String str = "";try {str = br.readLine();} catch (IOException e) {e.printStackTrace();}return str;}} public static void main(String[] args)throws IOException{try{boolean oj=true;if(oj==true) {AnotherReader sk=new AnotherReader();PrintWriter out=new PrintWriter(System.out);} else {AnotherReader sk=new AnotherReader(100);out=new PrintWriter("output.txt");} {process();}out.flush();out.close();}catch(Exception e){return;}}} //*-----------------------------------------------------------------------------------------------------------------------------------*//
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() n = len(s) ans = 0 m = 0 for i in range(n - 5, -1, -1): if s[i:i + 5] == "metal": m += 1 elif s[i:i + 5] == "heavy": ans += m print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; const long long int mymax = 1e18; const long long int mymin = -1e18; const double PIE = 3.1415926536; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s, a = "heavy", b = "metal"; cin >> s; long int slen = s.length(); long long int ans = 0; if (slen < 10) { cout << 0; return 0; } vector<long int> h, m; int tmp; bool valid; for (long int i = 0; i < slen - 4; i++) { if (s[i] == 'h') { tmp = 1; valid = true; i++; while (i < slen && tmp < 5) { if (s[i] != a[tmp]) { valid = false; break; } tmp++; i++; } if (valid) { h.push_back(i - 5); } i--; } else if (s[i] == 'm') { tmp = 1; valid = true; i++; while (i < slen && tmp < 5) { if (s[i] != b[tmp]) { valid = false; break; } tmp++; i++; } if (valid) { m.push_back(i - 5); } i--; } } if (h.empty() || m.empty()) { cout << 0; return 0; } long int msz = m.size(); for (auto ht = h.begin(); ht != h.end(); ht++) { ans += (long long int)(msz - (lower_bound(m.begin(), m.end(), *ht) - m.begin())); } cout << ans; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; const int oo = (int)(1e9); const long double PI = 3.141592653589793; int PP = 1000000007; const int MAXN = 111111 * 2, MAXE = 111111; template <typename TT> void read(TT &x) { char ch; for (ch = getchar(); ch > '9' || ch < '0'; ch = getchar()) ; for (x = 0; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48; } int n, m, k; char s[1111111]; char a[5], b[5]; int f[1111111], g[1111111]; void Init() { scanf("%s", s); ; n = strlen(s); a[0] = 'h'; a[1] = 'e'; a[2] = 'a'; a[3] = 'v'; a[4] = 'y'; b[0] = 'm'; b[1] = 'e'; b[2] = 't'; b[3] = 'a'; b[4] = 'l'; f[0] = g[0] = 0; for (int i = 0; i < n; i++) { bool ok = 1; for (int j = 0; j < 5; j++) ok &= (a[j] == s[j + i]); if (ok) f[i] = 1; ok = 1; for (int j = 0; j < 5; j++) ok &= (b[j] == s[j + i]); if (ok) g[i]++; g[i + 1] = g[i]; } long long ans = 0; for (int i = 0; i < n; i++) if (f[i]) { ans += g[n] - g[i]; } cout << ans; } int main() { Init(); }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s = raw_input() p = 0 for i,c in enumerate(s.split('heavy')): p += c.count('metal') * i print p
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; string str; int main() { ios_base::sync_with_stdio(0); getline(cin, str); long long res = 0; long long hv = 0; for (int i = 0; i + 5 <= str.size(); i++) { if (str.substr(i, 5) == "heavy") hv++; if (str.substr(i, 5) == "metal") res += hv; } 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
import java.util.Scanner; public class HeavyMetal { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String test = scan.next(); int h = 0; long ans = 0; for (int i = 5; i <= test.length(); i++) { if ( test.substring(i-5, i).equals("heavy") ) ++h; else if ( test.substring(i-5, i).equals("metal") ) ans += h; } System.out.println(ans); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.*; import java.util.*; //Codeforces public class Codeforces1 { private static MyScanner in; private static PrintStream out; private static boolean LOCAL_TEST = false; private static void solve() throws IOException { String s = in.nextString(); long cnt = 0; ArrayList<Integer> heavyPos = new ArrayList<Integer>(); ArrayList<Integer> metalPos = new ArrayList<Integer>(); int fromIndex = 0; while (fromIndex < s.length()) { int pos = s.indexOf("heavy", fromIndex); if (pos == -1) break; else { heavyPos.add(pos); fromIndex = pos + 5; } } fromIndex = 0; while (fromIndex < s.length()) { int pos = s.indexOf("metal", fromIndex); if (pos == -1) break; else metalPos.add(pos); fromIndex = pos + 5; } HashMap<Integer, Integer> firstMetalPosLargerThanHeavyPos = new HashMap<Integer, Integer>(); int lastMetalIdx = 0; for (int i = 0; i < heavyPos.size(); i++) { int hpos = heavyPos.get(i); firstMetalPosLargerThanHeavyPos.put(hpos, -1); for (int j = lastMetalIdx; j < metalPos.size(); j++) { if (metalPos.get(j) > heavyPos.get(i)) { firstMetalPosLargerThanHeavyPos.put(hpos, metalPos.get(j)); lastMetalIdx = j; break; } } } HashMap<Integer, Integer> cntMetalsLargerThanPos = new HashMap<Integer, Integer>(); for (int i = 0; i < metalPos.size(); i++) { int pos = metalPos.get(i); cntMetalsLargerThanPos.put(pos, metalPos.size() - 1 - i); } for (int i = 0; i < heavyPos.size(); i++) { int hpos = heavyPos.get(i); int firstMetalLarger = firstMetalPosLargerThanHeavyPos.get(hpos); if (firstMetalLarger < 0) continue; long cntMetalLarger = cntMetalsLargerThanPos.get(firstMetalLarger); cnt += cntMetalLarger + 1; } out.println(cnt); } public static void main(String[] args) throws IOException { // helpers for input/output out = System.out; try { String cname = System.getenv("COMPUTERNAME"); if (cname != null) LOCAL_TEST = true; } catch (Exception e) { } if (LOCAL_TEST) { in = new MyScanner("E:\\zin.txt"); } else { boolean usingFileForIO = false; if (usingFileForIO) { // using input.txt and output.txt as I/O in = new MyScanner("input.txt"); out = new PrintStream("output.txt"); } else { in = new MyScanner(); out = System.out; } } solve(); } // ===================================== static class MyScanner { BufferedReader bufReader; StringTokenizer strTok; public MyScanner() throws IOException { bufReader = new BufferedReader(new InputStreamReader(System.in)); strTok = new StringTokenizer(""); } public MyScanner(String inputFile) throws IOException { bufReader = new BufferedReader(new InputStreamReader(new FileInputStream( inputFile))); strTok = new StringTokenizer(""); } String GetNextToken() throws IOException { if (!strTok.hasMoreTokens()) strTok = new StringTokenizer(bufReader.readLine()); return strTok.nextToken(); } public int nextInt() throws IOException { return Integer.valueOf(GetNextToken()); } public long nextLong() throws IOException { return Long.valueOf(GetNextToken()); } public double nextDouble() throws IOException { return Double.valueOf(GetNextToken()); } public String nextString() throws IOException { return GetNextToken(); } public String nextLine() throws IOException { return bufReader.readLine(); } public int countTokens() { return strTok.countTokens(); } public boolean hasMoreTokens() { return strTok.hasMoreTokens(); } } }
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; using ll = long long; const int N = 2e6 + 10; const string t = "heavy", tt = "metal"; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; ll ans = 0; map<string, ll> mp; if (s.size() < 5) cout << 0 << endl, exit(0); else { for (int i = 0; i <= s.size() - 5; i++) { string o = s.substr(i, 5); if (o == t) mp[t]++; else if (o == tt) ans += mp[t]; } } cout << ans << endl; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys s = sys.stdin.readline().split("heavy") metals = 0 total = 0 for i in xrange(len(s) - 1, 0, -1): metals = metals + s[i].count("metal") total = total + metals print total
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s=input() n=len(s) ans,cnt=0,0 for i in range(n-4): if s[i:i+5]=="heavy":cnt+=1 elif s[i:i+5]=='metal':ans+=cnt print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
def cal(heavy, metal): Res = 0 for i in range(len(heavy)): left = 0 right = len(metal) - 1 while left <= right: mid = (left + right) // 2 if metal[mid] > heavy[i]: if (metal[mid - 1] < heavy[i]) | (mid == 0): Res = Res + len(metal) - mid break else: right = mid - 1 else: left = mid + 1 return Res s = raw_input().strip() heavy = [] metal = [] for i in range(len(s) - 4): if s[i: i + 5] == 'heavy': heavy.append(i) if s[i: i + 5] == 'metal': metal.append(i) print cal(heavy, metal)
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; public class B { public static void main(String[] args) throws IOException { BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); char s[]=reader.readLine().toCharArray(); long heavyCount=0; long ans=0; char heavy[]={'h','e','a','v','y'}; char metal[]={'m','e','t','a','l'}; for(int i=0;i<s.length-5+1;i++) { if(s[i]=='h') { int temp=i; int j=0; while(j<heavy.length && s[i]==heavy[j]){ i++; j++; } // System.out.println(temp+" "+j); if(j==heavy.length) { heavyCount++; // i+=hea; } i=temp; } else if(s[i]=='m') { int temp=i; int j=0; //System.out.println(i+" "+j); while(j<metal.length&&s[i]==metal[j]){ i++; j++; } if(j==metal.length){ ans+=heavyCount; // i+=metal.length-1; } i=temp; //System.out.println(i); } } System.out.println(ans); } } //heavymetmetalmetal
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; public class StringsOfPower { static char[] arr; public static void main(String[] args) { Scanner sc = new Scanner(System.in); arr = sc.nextLine().toCharArray(); long add = 0; long result = 0; for (int i = 0; i < arr.length; i++) { if(heavy(i)) { add++; i += 4; } else if(metal(i)) { result += add; i+=4; } } System.out.println(result); } public static boolean heavy(int i) { if(arr.length - i < 5) return false; else if(arr[i] == 'h' && arr[i+1] == 'e' && arr[i+2] == 'a' && arr[i+3] == 'v' && arr[i+4] == 'y') return true; else return false; } public static boolean metal(int i) { if(arr.length - i < 5) return false; else if(arr[i] == 'm' && arr[i+1] == 'e' && arr[i+2] == 't' && arr[i+3] == 'a' && arr[i+4] == 'l') return true; else return false; } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s=input() c=0;t=0; for i in range(len(s)): if s[i:i+5]=="heavy": c+=1 elif s[i:i+5]=="metal": t+=c print(t)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s = raw_input() lens = len(s) x,ans,i = [0]*3 while(i<len(s)): if(s[i:i+5]=="heavy"): x+=1 if(s[i:i+5]=="metal"): ans+=x i+=1 print ans
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys from collections import Counter input = sys.stdin.readline printt = sys.stdout.write ############ ---- Input Functions ---- ############ def aa(): #for int return(int(input())) def bb(): #for int arr return(list(map(int,input().split()))) def cc(): #for string s = input() return s #(list(s[:len(s) - 1])) def dd(): #space seperated return(map(int,input().split())) """ for printing list: print(" ".join(list(map(str,list)))+"\n") for printing num : print(str(n)+"\n") for printing string : print(str+"\n") """ s = cc() h=0 m=0 for i in range(len(s)-4): if s[i:i+5]=="heavy": h+=1 elif s[i:i+5]=="metal": m+=h print(str(m)+"\n")
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() n=len(s) i=0 heavycount=0 totalcount=0 while i<=n-5: if s[i:i+5]=="heavy": heavycount=heavycount+1 #print("heavy",heavycount) i=i+5 elif s[i:i+5]=="metal": totalcount=totalcount+heavycount #print("metal",totalcount) i=i+5 else: i=i+1 print(totalcount)
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 om { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String x=sc.next().replace("heavy",".").replace("metal","?"); int dot=0; long ans=0; for(int i=0;i<x.length();i++){ if(x.charAt(i)=='.'){ dot++; } else if(x.charAt(i)=='?'){ ans=ans+dot; } } System.out.println(ans); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.StringTokenizer; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author utp */ public class CodeB { static class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String nextLine() { try { return br.readLine(); } catch(Exception e) { throw(new RuntimeException()); } } public String next() { while(!st.hasMoreTokens()) { String l = nextLine(); if(l == null) return null; st = new StringTokenizer(l); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < res.length; i++) res[i] = nextInt(); return res; } public long[] nextLongArray(int n) { long[] res = new long[n]; for(int i = 0; i < res.length; i++) res[i] = nextLong(); return res; } public double[] nextDoubleArray(int n) { double[] res = new double[n]; for(int i = 0; i < res.length; i++) res[i] = nextDouble(); return res; } public void sortIntArray(int[] array) { Integer[] vals = new Integer[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortLongArray(long[] array) { Long[] vals = new Long[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public void sortDoubleArray(double[] array) { Double[] vals = new Double[array.length]; for(int i = 0; i < array.length; i++) vals[i] = array[i]; Arrays.sort(vals); for(int i = 0; i < array.length; i++) array[i] = vals[i]; } public String[] nextStringArray(int n) { String[] vals = new String[n]; for(int i = 0; i < n; i++) vals[i] = next(); return vals; } Integer nextInteger() { String s = next(); if(s == null) return null; return Integer.parseInt(s); } int[][] nextIntMatrix(int n, int m) { int[][] ans = new int[n][]; for(int i = 0; i < n; i++) ans[i] = nextIntArray(m); return ans; } static <T> T fill(T arreglo, int val) { if(arreglo instanceof Object[]) { Object[] a = (Object[]) arreglo; for(Object x : a) fill(x, val); } else if(arreglo instanceof int[]) Arrays.fill((int[]) arreglo, val); else if(arreglo instanceof double[]) Arrays.fill((double[]) arreglo, val); else if(arreglo instanceof long[]) Arrays.fill((long[]) arreglo, val); return arreglo; } <T> T[] nextObjectArray(Class <T> clazz, int size) { @SuppressWarnings("unchecked") T[] result = (T[]) java.lang.reflect.Array.newInstance(clazz, size); try { Constructor <T> constructor = clazz.getConstructor(Scanner.class, Integer.TYPE); for(int i = 0; i < result.length; i++) result[i] = constructor.newInstance(this, i); return result; } catch(Exception e) { throw new RuntimeException(e); } } } static boolean starts(char[] a, char[] b, int pos) { if(pos < 0 || pos >= a.length) return false; if(pos + b.length > a.length) return false; for(int i = 0; i < b.length; i++) if(a[i + pos] != b[i]) return false; return true; } public static void main(String[] args) { Scanner sc = new Scanner(); char[] texto = sc.next().toCharArray(); boolean[] empieza = new boolean[texto.length]; boolean[] termina = new boolean[texto.length]; char[] inicio = {'h', 'e', 'a', 'v', 'y'}; char[] fin = {'m', 'e', 't', 'a', 'l'}; for(int i = 0; i < empieza.length; i++) { empieza[i] = starts(texto, inicio, i); termina[i] = starts(texto, fin, i - fin.length + 1); } long total = 0; int actual = 0; for(int i = empieza.length - 1; i >= 0; i--) { if(empieza[i]) total += actual; if(termina[i]) actual++; } 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
#include <bits/stdc++.h> using namespace std; long long fast_power_mod(long long b, long long p, long long m) { if (p == 0) return 1; long long res = fast_power_mod(b, p / 2, m) % m; if (p % 2 == 0) return ((res) * (res)) % m; else return ((res) * (res) * (b % m)) % m; } long long arthmetic_seq_sum(long long start, long long end, long long terms) { return ((start + end) * terms) / 2; } long long fast_power(long long b, long long p) { if (p == 0) return 1; long long res = fast_power(b, p / 2); if (p % 2 == 0) return res * res; else return res * res * b; } bool comp(pair<int, int>* a, pair<int, int>* b, int n) { for (int i = 0; i < n; i++) if (a[i].first != b[i].first) return false; return true; } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); string x; cin >> x; long long cnt = 0, n = x.size(); vector<int> heavy, metal; for (int i = 0; i <= n - 5; i++) { if (x.substr(i, 5) == "heavy") heavy.push_back(i); else if (x.substr(i, 5) == "metal") metal.push_back(i); } for (int i = 0; i < heavy.size(); ++i) { auto add = upper_bound(metal.begin(), metal.end(), heavy[i]); if (add == metal.end()) break; int index = add - metal.begin(); cnt += metal.size() - index; } 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() { string s; long long c = 0, k = 0; cin >> s; if (s.length() < 5) { cout << 0 << endl; return 0; } for (int i = 0; i < s.length() - 4; i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') k++; if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') c += k; } cout << c << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class B { public static void main(String[] args){ FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); String s = sc.nextToken(); List<Integer> heavy = new ArrayList<Integer>(); List<Integer> metal = new ArrayList<Integer>(); String heavyS = "heavy"; String metalS = "metal"; for(int i=0 ; i<s.length()-4; i++){ boolean isHeavy = true; boolean isMetal = true; for(int j = 0; j<= 4; j++){ if(heavyS.charAt(j) != s.charAt(i+j)){ isHeavy = false; } if(metalS.charAt(j) != s.charAt(i+j)){ isMetal = false; } } if(isHeavy){ heavy.add(i); } if(isMetal){ metal.add(i); } } int curH = 0; int curM = 0; long count = 0; if(!(heavy.size() == 0 || metal.size() == 0)){ while(true){ if(heavy.get(curH) < metal.get(curM)){ count += (long)(metal.size() - curM); curH++; }else{ curM++; } if(curH == heavy.size() || curM == metal.size()){ break; } } } out.println(count); out.close(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main{ static PrintWriter out; static BufferedReader in; public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); String str = in.readLine(); List<Integer> heavies = new ArrayList<Integer>(); List<Integer> metals = new ArrayList<Integer>(); int idx = str.indexOf("heavy", 0); while(idx>=0) { heavies.add(idx); idx = str.indexOf("heavy", idx + 5); } idx = str.indexOf("metal"); while(idx>=0) { metals.add(idx); idx = str.indexOf("metal", idx + 5); } long ans = 0; for (int i = 0; i < heavies.size(); i++) { long index = Collections.binarySearch( metals, heavies.get(i) ); index = -index - 1 ; ans += metals.size() - index ; } out.println(ans); out.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
#itne me hi thak gaye? from bisect import bisect_left s = input() h = [i for i in range(len(s)) if s.startswith('heavy', i)] m = [i for i in range(len(s)) if s.startswith('metal', i)] # print(h, m) ans = 0 for i in h: ans += len(m) - bisect_left(m, i) print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s = raw_input(); len_s = len(s) heavy_amount, all = 0, 0; for i in xrange(len_s-4): if s[i:i+5] == "heavy": heavy_amount += 1; elif s[i:i+5] == "metal": all += heavy_amount; print all;
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.ArrayList; import java.util.Arrays; public class Main { static int [] heavyar; static int [] metalar; static int found=-1; public static void main(String [] args) throws IOException { BufferedReader br1= new BufferedReader(new InputStreamReader(System.in)); String data = br1.readLine(); long count =0; heavyar = new int[200000]; metalar = new int[200000]; int heavylast=0; int metallast=0; int k =0; int f=0; boolean ispresent=false; boolean isthere= false; for (int i = 0; i <= data.length()-1-4; i++) { String fetch = data.substring(i,i+5); if(fetch.equals("heavy")) { heavyar[k]=i; heavylast=k; k++; ispresent = true; //System.out.println(i); } else if(fetch.equals("metal")) { metalar[f]=i; metallast=f; f++; isthere = true; //System.out.println(i); } } //int msize= metal.size(); //System.out.println("heavy"+Arrays.toString(heavyar)); //System.out.println("metal"+Arrays.toString(metalar)); for (int i = 0; (i <= heavylast) && (ispresent)&& (isthere); i++) { //System.out.println(metal.size()-j); binar(0,metallast,heavyar[i]); //System.out.println(found); //System.out.println(found); if( found==-1) { } else count+=metallast- found +1; } System.out.println(count); } static void binar(int i,int length,int required) { int middle = i + (length-i)/2; //System.out.println(i+ " "+length); if(i==length ) { if(metalar[i]<required) found=-1; else found=i; return; } if(required>metalar[middle]) { binar(middle+1,length,required); } else { binar(i,middle,required); } } }
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 n, i, j, cnth = 0, cntm = 0, flag1 = 0, flag2 = 0; long long ans = 0; cin >> s; n = s.size(); for (i = 0; i < n - 4; i++) { if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') { flag1 = 1; cnth++; } if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { flag2 = 1; if (flag1 == 1) { ans += cnth; } } } cout << ans << "\n"; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s=input() a,b=0,0 n=len(s) for i in range(n): if(s[i:i+5]=='heavy'): a=a+1 elif(s[i:i+5]=='metal'): b=b+a print(b)
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 dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; string str; int n; bool arr[1100000], brr[1100000]; long long cum[1100000]; void check(int idx) { if (idx + 5 <= str.size()) { if (str.substr(idx, 5) == string("heavy")) arr[idx] = 1; if (str.substr(idx, 5) == string("metal")) brr[idx] = 1; } } int main() { memset(arr, 0, sizeof arr); memset(brr, 0, sizeof brr); cin >> str; n = str.size(); int cnt = 0; for (int i = (0); i < (n); i++) { check(i); } cum[n - 1] = brr[n - 1]; for (int i = (n - 2); i >= (0); i--) { cum[i] = cum[i + 1] + brr[i]; } long long ans = 0; for (int i = (0); i < (n); i++) { if (arr[i]) ans += cum[i]; } cout << ans << endl; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) { Scanner scan = new Scanner(System.in); char[] s = scan.nextLine().toCharArray(); ArrayList<Integer> a = new ArrayList<Integer>(); if(s.length <= 9) System.out.println("0"); else { for(int i=0; i<s.length - 4; i++) { if(s[i] == 'h') if(s[i+1] == 'e') if(s[i+2] == 'a') if(s[i+3] == 'v') if(s[i+4] == 'y') a.add(1); if(s[i] == 'm') if(s[i+1] == 'e') if(s[i+2] == 't') if(s[i+3] == 'a') if(s[i+4] == 'l') a.add(0); } int zero = 0; for(int i=0; i<a.size(); i++) { if(a.get(i) == 0) zero++; } long ans = 0; for(int i=0; i<a.size() && zero > 0; i++) { if(a.get(i) == 1) ans += zero; else zero--; } System.out.println(ans); } } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; long long maxINT = 998244353; vector<int> kmp(string p, string cur) { string s = p + "#" + cur; int n = s.length(); vector<int> v(n); vector<int> ans; for (int i = 1; i < n; ++i) { int j = v[i - 1]; while (j != 0 && s[i] != s[j]) { j = v[j - 1]; } if (s[i] == s[j]) { j++; } v[i] = j; if (j == p.length()) { ans.push_back(i - p.length() - 1); } } return ans; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); string s; cin >> s; vector<int> h = kmp("heavy", s); vector<int> m = kmp("metal", s); int i = 0; int j = 0; long long ans = 0; while (i < h.size() && j < m.size()) { if (h[i] < m[j]) { ans += m.size() - j; i++; } else { j++; } } cout << ans; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; int r = 0; int z = a.size(); long long int x = 0; for (int i = 0; i <= z;) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') { r++; i += 4; } else if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') { x += r; i += 4; } i++; } cout << x; 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 = 1e6; bool a[maxn]; long long j = 0; string s; long long ans = 0; int main() { cin >> s; if (s.length() < 5) { printf("0\n"); return 0; } for (long long i = 0; i <= s.length() - 5; ++i) { if (s.substr(i, 5) == "heavy") a[j++] = 1; else if (s.substr(i, 5) == "metal") a[j++] = 0; } long long k = 0; for (long long i = j - 1; i >= 0; --i) { if (!a[i]) ++k; else ans += k; } printf("%I64d\n", ans); return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main extends PrintWriter { BufferedReader in; StringTokenizer stok; final Random rand = new Random(31); final int inf = (int) 1e9; final long linf = (long) 1e18; boolean f(char[] s, int off, char[] t) { for (int i = 0; i < t.length; i++) { if (t[i] != s[off + i]) { return false; } } return true; } void solve() throws IOException { char[] heavy = "heavy".toCharArray(); char[] metal = "metal".toCharArray(); char[] s = next().toCharArray(); long ans = 0; int op = 0; for (int i = 0; i < s.length - 4; i++) { if (f(s, i, heavy)) { op++; } if (f(s, i, metal)) { ans += op; } } println(ans); } void run() { try { solve(); } catch (Exception e) { e.printStackTrace(); System.exit(abs(-1)); } finally { close(); } } Main() throws IOException { super(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } Main(String filename) throws IOException { super("".equals(filename) ? "output.txt" : filename + ".out"); in = new BufferedReader(new FileReader("".equals(filename) ? "input.txt" : filename + ".in")); } public static void main(String[] args) throws IOException { new Main().run(); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } int[] nextIntArray(int len) throws IOException { int[] res = new int[len]; for (int i = 0; i < len; i++) { res[i] = nextInt(); } return res; } void shuffle(int[] a) { for (int i = 1; i < a.length; i++) { int x = rand.nextInt(i + 1); int _ = a[i]; a[i] = a[x]; a[x] = _; } } boolean nextPerm(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1; ; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } <T> List<T>[] createAdjacencyList(int countVertex) { List<T>[] res = new List[countVertex]; for (int i = 0; i < countVertex; i++) { res[i] = new ArrayList<T>(); } return res; } class Pair<A extends Comparable<A>, B extends Comparable<B>> implements Comparable<Pair<A, B>> { A a; B b; public Pair(A a, B b) { this.a = a; this.b = b; } @Override public int compareTo(Pair<A, B> o) { int aa = a.compareTo(o.a); return aa == 0 ? b.compareTo(o.b) : aa; } } }
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> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") const int INF = 1e9; const int N = 2e5 + 5; using namespace std; int Exp(int x, int y) { int ans = 1; for (int i = 1; i <= y; i++) ans *= x; return ans; } vector<bool> check_prime(N + 1, true); void Seive() { check_prime[0] = false; check_prime[1] = false; for (long long i = 2; i * i <= N; i++) { if (check_prime[i]) { for (long long j = i * i; j <= N; j += i) check_prime[j] = false; } } } bool prime(int x) { bool ok = true; if (x < 2) return false; if (x == 2) return true; else { for (int i = 2; i * i <= x; i++) { if (x % i == 0) { return false; } } return true; } } int dp[1000000]; int h[1000000]; void solve(int tc) { string s; cin >> s; long long x = 0; long long y = 0; for (long long i = 0; i <= s.size() - 1; i++) { if (s.substr(i, 5) == "metal") { y += x; } if (s.substr(i, 5) == "heavy") { x++; } } cout << y << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; for (int i = 1; i <= t; i++) { solve(i); } }
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; vector<int> pre; vector<int> post; for (int i = 0; i < s.length(); i++) { string p = ""; if (s[i] == 'h') { int j = i; int hl = 5; while (j < s.length() && hl) { p += s[j]; j++; hl--; } if (p == "heavy") { pre.push_back(i); p = ""; } } else if (s[i] == 'm') { int j = i; int ml = 5; while (j < s.length() && ml) { p += s[j]; j++; ml--; } if (p == "metal") { post.push_back(i); p = ""; } } } long long int count = 0; vector<int>::iterator upper1, upper2; for (int i = 0; i < pre.size(); i++) { upper1 = upper_bound(post.begin(), post.end(), pre[i]); count += post.size() - (upper1 - post.begin()); } cout << count << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.Scanner; import java.util.LinkedList; public class StringsofPower { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); s = s.replaceAll("heavy", "#"); s = s.replaceAll("metal", "*"); long numOfMetal = 0; for (int i = 0; i < s.length(); ++i) { if (s.charAt(i) == '*') { ++numOfMetal; } } long res = 0; for (int i = 0; i < s.length(); ++i) { if (s.charAt(i) == '#') { res += numOfMetal; } else if (s.charAt(i) == '*') { --numOfMetal; } } System.out.println(res); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; vector<int> start; vector<int> e; void find(string s) { string a; for (int i = 0; i <= (int)s.size() - 5; i++) { a = ""; a += s[i]; a += s[i + 1]; a += s[i + 2]; a += s[i + 3]; a += s[i + 4]; if (a == "heavy") { start.push_back(i); } else if (a == "metal") { e.push_back(i); } } } int main() { string s; cin >> s; find(s); if ((int)start.size() * (int)e.size() == 0) { cout << 0; return 0; } long long ans = 0; int n = (int)start.size(); int m = (int)e.size(); for (auto i : start) { auto low = lower_bound(e.begin(), e.end(), i); int ind = low - e.begin(); if (ind != m) { ans += (m - ind); } } cout << ans; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; char h[6] = "heavy"; char m[6] = "metal"; char s[1000005]; int main() { scanf("%s", s); int len = strlen(s); int i, j; i = j = 0; long long ans = 0; for (int k = 0; k < len; k++) { if (s[k] == h[0] && s[k + 1] == h[1] && s[k + 2] == h[2] && s[k + 3] == h[3] && s[k + 4] == h[4]) i++; if (s[k] == m[0] && s[k + 1] == m[1] && s[k + 2] == m[2] && s[k + 3] == m[3] && s[k + 4] == m[4]) { ans += i; } } printf("%I64d\n", ans); }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
def sol(s): h = "heavy" m = "metal" i = 0 j = 0 occ = "" for c in s: if c == h[i]: if i == len(h)-1: occ+="h" i = 0 else: i+=1 else: if c == h[0]: i = 1 else: i = 0 if c == m[j]: if j == len(m)-1: occ+="m" j = 0 else: j+=1 else: if c == m[0]: j = 1 else: j = 0 ans = 0 n = len(occ) count_m = 0 for c in range(n-1,-1,-1): if occ[c] == "m": count_m+=1 else: ans+=count_m return ans s = input() print(sol(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
//package codeforces; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: ANURAG * Date: 6/22/13 * Time: 12:32 AM * To change this template use File | Settings | File Templates. */ public class SuperString { private static long solveCase(String str) { int[] metalIndex = new int[str.length() + 1]; int[] heavyIndex = new int[str.length()]; char[] heavy = "heavy".toCharArray(); char[] metal = "latem".toCharArray(); metalIndex[str.length()] = 0; for (int i = str.length() - 1; i >= 4; i--) { int count = 0; for (int j = 0; j < metal.length; j++) { // System.out.println(metal[j]); if (str.charAt(i - j) == metal[j]) count++; else { // System.out.println("No Equal: " + str.charAt(i - j) + ", " + metal[j]); break; } } if (count == metal.length) metalIndex[i] = metalIndex[i + 1] + 1; else metalIndex[i] = metalIndex[i + 1]; } // count(str, heavy, heavyIndex); //System.out.println(Arrays.toString(metalIndex)); long netCount = 0; for (int i = 0; i < (str.length() - heavy.length + 1); i++) { int count = 0; for (int j = 0; j < heavy.length; j++) { if (str.charAt(i + j) == heavy[j]) count++; else break; } if (count == heavy.length) { // System.out.println("Found!"); netCount += metalIndex[i + heavy.length]; } } return netCount; } public static void getInput() { InputReader ir = new InputReader(System.in); String string = ir.next(); System.out.println(solveCase(string)); } static class InputReader { private BufferedReader br; private String inputFile = ""; private StringTokenizer sz; private final String DELIM = " "; public InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); sz = new StringTokenizer(inputFile, DELIM); } public String next() { if (sz.hasMoreTokens()) { return sz.nextToken(); } try { sz = new StringTokenizer(br.readLine(), DELIM); } catch (IOException ex) { } return sz.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] args) { SuperString.getInput(); } } class Main { public static void main(String[] args) { SuperString.getInput(); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { char s[1000009]; vector<int> v; scanf("%s", s); int i, j; long long int S = 0; for (i = 0; s[i]; i++) if (s[i] == 'h' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'v' && s[i + 4] == 'y') v.push_back(i); v.push_back(i); j = 0; for (i = 0; s[i]; i++) if (s[i] == 'm' && s[i + 1] == 'e' && s[i + 2] == 't' && s[i + 3] == 'a' && s[i + 4] == 'l') { while (v[j] < i) j++; S += j; } cout << S << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import re from collections import OrderedDict I=lambda:map(int, raw_input().split()) s=raw_input() x=[a.start() for a in list(re.finditer('heavy', s))] y=[a.start() for a in list(re.finditer('metal', s))] d=OrderedDict() prev=0 for a in x: d[a]=prev+1 prev=d[a] ans=0 items=d.items() n=len(items) i=0 for a in y: while i<n and items[i][0]<=a: i+=1 if items[i-1][0]<a: ans+=items[i-1][1] print ans
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int binary_search(int *arr, int n, int x) { int l = 0, r = n - 1, mid, ans = n; while (l <= r) { mid = (l + r) / 2; if (arr[mid] > x) { r = mid - 1; ans = mid; } else l = mid + 1; } return ans; } int main() { string s; cin >> s; int heavy[1000000] = {0}, metal[1000000] = {0}, m = 0, n = 0; for (int i = 0; i < s.length(); i++) { string temp = ""; if (i + 5 <= s.length()) temp = s.substr(i, 5); if (temp == "heavy") heavy[m++] = i; else if (temp == "metal") metal[n++] = i; } long long count = 0; for (int i = 0; i < m; i++) { int j = binary_search(metal, n, heavy[i]); count += (n - j); } 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
import java.io.*; import java.util.*; public class Main { FastReader scn; PrintWriter out; String INPUT = ""; void solve() { char[] arr = scn.next().toCharArray(); String h = "heavy", m = "metal"; int n = arr.length; StringBuilder sb = new StringBuilder(); int M = 0; for(int i = 0; i <= n - 5; i++) { boolean heavy = true; for(int j = i; j < i + 5; j++) { if(h.charAt(j - i) != arr[j]) { heavy = false; break; } } if(heavy) { sb.append('h'); i += 4; continue; } boolean metal = true; for(int j = i; j < i + 5; j++) { if(m.charAt(j - i) != arr[j]) { metal = false; break; } } if(metal) { M++; sb.append('m'); i += 4; continue; } } long ans = 0; for(int i = 0; i < sb.length(); i++) { if(sb.charAt(i) == 'm') { M--; } else { ans += M; } } out.println(ans); } void run() throws Exception { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) throws Exception { new Main().run(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } return arr; } } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
from sys import stdin,stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) for _ in range(1):#nmbr(): s=input() n=len(s) cnt=ans=0 for i in range(n): if s[i-4:i+1]=='heavy':cnt+=1 if s[i-4:i+1]=='metal':ans+=cnt print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.Scanner; public class Main { static String s; static long cnt = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); s = sc.next(); long sum = 0; for (int i = 0; i + 4 < s.length(); i++) { if ("heavy".equals(s.substring(i, i + 5))) { cnt++; } if ("metal".equals(s.substring(i, i + 5))) { sum += cnt; } } System.out.print(sum); sc.close(); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s,a ,ans = raw_input(),0 ,0 for i in xrange(len(s)-4): if s[i:i+5] == "heavy" : a+=1 elif s[i:i+5]=='metal': ans += a 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; public class B { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); char[] str = br.readLine().toCharArray(); long a = 0, ans = 0; for (int i = 0; i < str.length - 4; i++) { if (str[i] == 'h' && str[i + 1] == 'e' && str[i + 2] == 'a' && str[i + 3] == 'v' && str[i + 4] == 'y') { a++; } else if (str[i] == 'm' && str[i + 1] == 'e' && str[i + 2] == 't' && str[i + 3] == 'a' && str[i + 4] == 'l') { ans += a; } } System.out.println(ans); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; /*La clase debe siempre llamarse Main y se pΓΊblica*/ public class Main { public static void main(String[] args) throws Exception { /*Para las pruebas se usa un archivo de entrada, en este caso llamado "entrada", este archivo debe estar en el directorio del programa java o para los que usan Eclipse, en el directorio de proyecto*/ //File archivo = new File("in.txt"); //BufferedReader br = new BufferedReader(new FileReader (archivo)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String entrada; entrada = br.readLine(); String entrada2 = entrada; String pr1 = "metal"; String pr2 = "heavy"; long cont = 0; long cont2 = 0; for (int i = 1; i < entrada.length(); i++) { if (i + 4 <= entrada.length()) { if (entrada.substring(i - 1, i + 4).equals(pr1)) { i += 4; cont += cont2; } else { if (entrada.substring(i - 1, i + 4).equals(pr2)) { cont2++; i += 4; } } } } System.out.println(cont); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.awt.image.RescaleOp; import java.io.BufferedInputStream; import java.io.BufferedReader; 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.HashMap; import java.util.Scanner; import java.util.StringTokenizer; public class Main{ static long n,k,m,g,b; static long ans; static String string="heavymetal"; public static void main(String[] args) { //InputReader in=new InputReader(System.in); Scanner in=new Scanner(new BufferedInputStream(System.in)); PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); String s=in.next(); for(int i=0;i<s.length()-4;i++){ String t=s.substring(i,i+5); if(t.equals("heavy")){ n++; i+=4; }else if(t.equals("metal")){ ans+=n+m; m+=n; n=0; i+=4; } } System.out.println(ans); } // public static class T { // int a,b; //// T(int xx,int yy){ //// a=xx; //// b=yy; //// //// } // } // public static class cmp implements Comparator<T>{ // // public int compare(T a, T b) { // if(a.x!=b.x) // return a.x-b.x; // else // return a.y-b.y; // } // // } // public static class InputReader{ BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream){ reader=new BufferedReader(new InputStreamReader(System.in),32768); tokenizer=null; } public String next(){ while (tokenizer==null||!tokenizer.hasMoreTokens()) { try { tokenizer=new StringTokenizer(reader.readLine()); } catch (IOException e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public boolean ready() throws IOException{ return reader.ready(); } } }
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 = raw_input() s = sys.stdin.readline() #heavy = [] heavy_num = 0 total_sum = 0 n = len(s) i = 0 while i < n - 4: if s[i] == 'h': if s[i+1] == 'e': if s[i+2] == 'a': if s[i+3] == 'v': if s[i+4] == 'y': i += 5 heavy_num +=1 continue elif s[i] == 'm': if s[i+1] == 'e': if s[i+2] == 't': if s[i+3] == 'a': if s[i+4] == 'l': i+=5 total_sum += heavy_num continue i += 1 print total_sum
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; vector<string> pos; int main() { string str; while (cin >> str) { pos.clear(); for (int i = 0; i < str.size(); i++) { string flag = str.substr(i, 5); if (flag == "heavy") { pos.push_back("h"); i += 4; } else if (flag == "metal") { pos.push_back("m"); i += 4; } } long long count = 0; long long ans = 0; for (int i = pos.size() - 1; i >= 0; i--) { if (pos[i] == "m") count++; else ans += count; } cout << ans << endl; } return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; long long int h = 0, ans = 0, i; int len = (int)str.size(); for (int i = 0; i < len; i++) { if (str[i] == 'h' && str[i + 1] == 'e' && str[i + 2] == 'a' && str[i + 3] == 'v' && str[i + 4] == 'y') { h++; i += 4; } else if (str[i] == 'm' && str[i + 1] == 'e' && str[i + 2] == 't' && str[i + 3] == 'a' && str[i + 4] == 'l') { ans += h; } } 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
import java.util.*; import java.util.concurrent.ScheduledExecutorService; import java.io.*; import java.lang.*; import java.math.*; public class CodeChef { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); String str=scan.next(); int n= str.length(); int cnt=0; for(int i=0; i<=n-5; i++) { String temp = str.substring(i,i+5); if(temp.equals("metal")) cnt++; } long ans=0; for(int i=0; i<=n-5; i++) { String temp = str.substring(i,i+5); if(temp.equals("metal")) cnt--; if(temp.equals("heavy")) 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=input() sum=0 a=0 b=0 for i in range(len(s)-4): if(s[i:i+5:] == 'heavy'): a+=1 elif(s[i:i+5:] == 'metal'): b+=a print(b)
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.util.Scanner; public class ProB { static BufferedReader br; static long count = 0; public static void main(String[] args) { Scanner cin = new Scanner(System.in); try { check(cin.next().toCharArray()); } catch (IOException e) { e.printStackTrace(); } cin.close(); // System.out.println(heavy); // System.out.println(metal); System.out.println(count); } private static void check(char[] cs) throws IOException { long heavyCount = 0; for (int i = 0; i < cs.length - 4; i++) { if (cs[i] == 'h' && cs[i + 1] == 'e' && cs[i + 2] == 'a' && cs[i + 3] == 'v' && cs[i + 4] == 'y') { heavyCount++; i += 4; } else if (cs[i] == 'm' && cs[i + 1] == 'e' && cs[i + 2] == 't' && cs[i + 3] == 'a' && cs[i + 4] == 'l') { count += heavyCount; i += 4; } } } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; /** * * @author Donato */ public class StringsOfPower { public static long base = 128L; public static int tope = 1000000+10; static long[] powB = new long[tope]; static long[] h = new long[tope]; public static long hash(int i, int j){ return h[i] - h[j+1]*powB[j-i+1]; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String p=br.readLine(); char[] lista=p.toCharArray(); int largo = p.length(); //Calcular hash h[largo-1] = lista[largo-1]; char[] heavy_c="heavy".toCharArray(); char[] metal_c="metal".toCharArray(); long hash_metal='l'; for(int i=3;i>=0;i--){ hash_metal=metal_c[i]+base*hash_metal; } long hash_heavy='y'; for(int i=3;i>=0;i--){ hash_heavy=heavy_c[i]+base*hash_heavy; } powB[0] = 1L; powB[1] = base; for(int i = largo-2; i>=0; i--){ h[i] = lista[i] + base*h[i+1]; powB[largo-i] = powB[largo-i-1]*base; } ArrayList<Integer> metal=new ArrayList<>(); ArrayList<Integer> heavy=new ArrayList<>(); long cant_heavy=0; long cant_metal=0; for(int i=0;i<largo-4;i++){ if(hash(i,i+4)==hash_metal){ metal.add(i); cant_metal++; } else if(hash(i,i+4)==hash_heavy){ heavy.add(i); cant_heavy++; } } int i=0; int j=0; long contador=0; while(j<cant_metal && i<cant_heavy){ if(heavy.get(i)<=metal.get(j)){ contador=contador+cant_metal-j; i++; } else{ j++; } } System.out.println(contador); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long ans = 0, h = 0; if (s.size() < 10) { cout << 0 << endl; return 0; } for (int i = 0; i < s.size() - 4; i++) { if (s.substr(i, 5) == "heavy") { h++; } if (s.substr(i, 5) == "metal") { 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
s = raw_input() str = "" for i in xrange(len(s)-4): t = s[i:i+5] if t == "heavy": str += "1" elif t == "metal": str += "2" count_1 = 0 res = 0 for num in str: if num == "1": count_1 += 1 elif num == "2": res += count_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 io,os,sys,atexit input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline @atexit.register def write(): sys.__stdout__.write(io.StringIO().getvalue()) from collections import deque as que, defaultdict as vector from bisect import bisect as bsearch from heapq import* inin = lambda: int(input()) inar = lambda: list(map(int,input().split())) inst= lambda: input().decode().rstrip('\n\r') INF=float('inf') '''from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc''' _T_=1 #inin() for _t_ in range(_T_): s=inst() hv=0 ans=0 for i in range(len(s)-4): if s[i:i+5]=='heavy': hv+=1 if s[i:i+5]=='metal': ans+=hv print(ans)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys, math def rs(): return sys.stdin.readline().strip() def ri(): return int(sys.stdin.readline().strip()) def ras(): return list(sys.stdin.readline().strip()) def rai(): return map(int,sys.stdin.readline().strip().split()) def solve(): s = rs() s = s.replace("heavy", "$").replace("metal", "!") ls = len(s) count = 0 c = 0 for i in xrange(ls): si = s[ls-i-1] if si == "!":c += 1 elif si == "$":count += c return count print solve()
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long int N = s.length(); string a = "heavy"; string b = "metal"; vector<int> hea; vector<int> me; for (int i = 0; i < N; i++) { if (s[i] == 'h') { string init = ""; if (i + 4 < N) { for (int j = i; j <= i + 4; j++) { init = init + s[j]; } } if (init == a) { hea.push_back(i); } } } for (int i = 0; i < N; i++) { if (s[i] == 'm') { string init = ""; if (i + 4 < N) { for (int j = i; j <= i + 4; j++) { init = init + s[j]; } if (init == b) { me.push_back(i); } } } } long long int ans = 0; for (int i = 0; i < hea.size(); i++) { long long int j = lower_bound(me.begin(), me.end(), hea[i]) - me.begin(); if (j < me.size()) { ans = ans + (me.size() - j); } } cout << ans << endl; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long h = 0, m = 0; for (long long i = 0; i < s.size(); i++) { if (s.substr(i, 5) == "heavy") h += 1; else if (s.substr(i, 5) == "metal") m += h; } cout << m << "\n"; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int gcd(int m, int n) { if (m % n == 0) return n; else gcd(n % m, m); } int main() { string a; while (cin >> a) { long long i, j, k, s = 0, p = 0, l; l = a.size(); for (int i = 0; i < l; i++) { if (a[i] == 'h' && a[i + 1] == 'e' && a[i + 2] == 'a' && a[i + 3] == 'v' && a[i + 4] == 'y') { i += 4; s++; } if (a[i] == 'm' && a[i + 1] == 'e' && a[i + 2] == 't' && a[i + 3] == 'a' && a[i + 4] == 'l') { p = p + s; i += 4; } } cout << p << "\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 from math import* s=input() ch=0 cm=0 ans=[] z=0 for i in range(len(s)-4): if s[i:i+5]=="heavy": z=1 ans.append(ch*cm) cm=0 ch+=1 elif s[i:i+5]=="metal": if z==1: cm+=1 ans.append(ch*cm) #print(ans) print(sum(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() mask1 = "heavy" mask2 = "metal" index1= [] index2 = [] def smallestbigger(arr, left, right, element): if left == right: return left if left < right: mid = (left+right)//2 if arr[mid] == element: return mid if arr[mid] > element: return smallestbigger(arr, left, mid, element) else: return smallestbigger(arr, mid+1, right, element) for i in range(len(s)-len(mask1)+1): if s[i:i+5] == mask1: index1.append(i) elif s[i:i+5] == mask2: index2.append(i) #print(index1) #print(index2) total = 0 for i in index1: total+=(len(index2)-smallestbigger(index2, 0, len(index2), i)) print(total)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import re data = raw_input() a = 'heavy' b = 'metal' apos = [m.start() for m in re.finditer(a, data)] bpos = [m.start() for m in re.finditer(b, data)] result = 0 if len(apos) and len(bpos): i = j = 0 while j<len(bpos): while i<len(apos) and apos[i] < bpos[j]: i += 1 result += i j += 1 print result
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys text = sys.stdin.readline().strip() i = 0 heavys = 0 result = 0 while (i + 5) <= len(text): if text[i:i+5] == "heavy": heavys +=1 i += 5 elif text[i:i+5] == "metal": result += heavys i += 5 else: i += 1 print(result)
PYTHON3
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import java.util.Scanner; public class cf{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); String s=sc.next(),t=""; int heavy=0,metal=0,temp=0; long cnt=0L; for(int i=0;i<s.length()-4;i++){ t=s.substring(i,i+5); if(t.equals("heavy")){ temp++; i+=4; } else if(t.equals("metal")){ i+=4; cnt+=temp+heavy; heavy+=temp; temp=0; } } 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 java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; import static java.lang.Math.*; public class Main { public static void solve(FastScanner fscan, BufferedWriter bw) throws IOException { String s = fscan.nextLine(); long ans = 0; int heavyFound = 0; String heavy = "heavy", metal = "metal"; int hMatch = 0, mMatch = 0; for (int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'h') { hMatch = 1; mMatch = 0; } else if(s.charAt(i) == 'm') { mMatch = 1; hMatch = 0; } else if(hMatch > 0) { if(s.charAt(i) == heavy.charAt(hMatch)) { hMatch++; if(hMatch == heavy.length()) { heavyFound++; hMatch = 0; } } else { hMatch = 0; } } else if(mMatch > 0) { if(s.charAt(i) == metal.charAt(mMatch)) { mMatch++; if(mMatch == metal.length()) { ans += heavyFound; mMatch = 0; } } else { mMatch = 0; } } } bw.append(String.valueOf(ans)); } /**************** ENTER HERE ********************/ public static void main(String[] args) throws Exception { if(args.length != 0){ test(); return; } FastScanner fscan = new FastScanner(System.in); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); solve(fscan,bw); bw.close(); } /**** Test Static test cases **/ public static void test() throws IOException{ String testcase[] = new String[]{}; String answer[] = new String[]{}; for (int i = 0; i < testcase.length; i++) { ByteArrayOutputStream result = new ByteArrayOutputStream(); FastScanner fscan = new FastScanner(new ByteArrayInputStream(testcase[i].getBytes())); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(result)); solve(fscan, bw); String r = new String(result.toByteArray()); if(! r.equals(answer[i])){ String q = testcase[i]; String a = answer[i]; System.err.println("Wrong Answer on test:"+(i+1)); System.err.println("Q:"+q); System.err.println("A:"+a); System.err.println("R:"+r); } } } } class FastScanner { InputStream is; byte buff[] = new byte[1024]; int currentChar = -1; int buffChars = 0; public FastScanner(InputStream inputStream) { is = inputStream; } public boolean hasNext() throws IOException { return currentChar == -1 || buffChars > 0; } public char nextChar() throws IOException { // if we already have that next char read, just return else input if (currentChar == -1 || currentChar >= buffChars) { currentChar = 0; buffChars = is.read(buff); } if (buffChars <= 0) { throw new RuntimeException("No char found..."); } int ch; while (isSpace(ch = nextCharAsInt())) ; return (char) ch; } public int nextCharAsInt() throws IOException { // if we already have that next char read, just return else input if (currentChar == -1 || currentChar >= buffChars) { currentChar = 0; buffChars = is.read(buff); } if (buffChars <= 0) { return -1; } return (char) buff[currentChar++]; } public String nextLine() throws IOException { StringBuilder bldr = new StringBuilder(); int ch; while (isLineEnd(ch = nextCharAsInt())) // ignore empty lines ??? ; do { bldr.append((char) ch); } while (!isLineEnd(ch = nextCharAsInt())); return bldr.toString(); } public String nextString() throws IOException { StringBuilder bldr = new StringBuilder(); int ch; while (isSpace(ch = nextCharAsInt())) ; do { bldr.append((char) ch); } while (!isSpace(ch = nextCharAsInt())); return bldr.toString(); } public int nextInt() throws IOException { // considering ASCII files--> 8 bit chars, unicode files has 16 bit // chars (byte1 then byte2) int result = 0; int sign = 1; int ch; while (isSpace(ch = nextCharAsInt())) ; if (ch == '-') { sign = -1; ch = nextCharAsInt(); } do { if (ch < '0' || ch > '9') throw new NumberFormatException("Found '" + ch + "' while parsing for int."); result *= 10; result += ch - '0'; } while (!isSpace(ch = nextCharAsInt())); return sign * result; } public long nextLong() throws IOException { // considering ASCII files--> 8 bit chars, unicode files has 16 bit // chars (byte1 then byte2) long result = 0; int sign = 1; int ch; while (isSpace(ch = nextCharAsInt())) ; if (ch == '-') { sign = -1; ch = nextCharAsInt(); } do { if (ch < '0' || ch > '9') throw new NumberFormatException("Found '" + ch + "' while parsing for int."); result *= 10; result += ch - '0'; } while (!isSpace(ch = nextCharAsInt())); return sign * result; } private boolean isLineEnd(int ch) { return ch == '\n' || ch == '\r' || ch == -1; } private boolean isSpace(int ch) { return ch == '\n' || ch == ' ' || ch == '\t' || ch == '\r' || ch == -1; } public void close() throws IOException { is.close(); } }
JAVA
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long ans = 0, cnt = 0; for (long long i = 0; i < s.size(); i++) { string check = s.substr(i, 5); if (check == "heavy") cnt++; if (check == "metal") ans += cnt; } cout << ans; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long int ans = 0, c = 0; for (int i = 4; i < s.length(); i++) { if (s[i] == 'l' && s[i - 4] == 'm' && s[i - 3] == 'e' && s[i - 2] == 't' && s[i - 1] == 'a') ans += c; else if (s[i] == 'y' && s[i - 4] == 'h' && s[i - 3] == 'e' && s[i - 2] == 'a' && s[i - 1] == 'v') ++c; } cout << ans << endl; return 0; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
s = raw_input() n = len(s) ends = 0 for i in xrange(n): t = s[i:i+5] if t == 'metal': ends += 1 answer = 0 for i in xrange(n): t = s[i:i+5] if t == 'heavy': answer += ends if t == 'metal': ends -= 1 print answer
PYTHON
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
#include <bits/stdc++.h> using namespace std; inline int in() { int32_t x; scanf("%d", &x); return x; } inline string get() { char ch[1000010]; scanf("%s", ch); return ch; } const int MAX_LG = 21; const long long maxn = 1e3 + 10; const long long base = 29; const long long mod = 1e9 + 7; const long long INF = 1e9 + 100; long long cnt = 0; long long res; int32_t main() { string s = get(); long long sz = s.size(); for (long long i = 0; i < sz; i++) { if (i + 4 < sz && s.substr(i, 5) == "heavy") cnt++; if (i + 4 < sz && s.substr(i, 5) == "metal") { res += cnt; } } cout << res << "\n"; }
CPP
318_B. Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number β€” the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input heavymetalisheavymetal Output 3 Input heavymetalismetal Output 2 Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Output 3 Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
2
8
import sys s = sys.stdin.readline() l = len(s) i = length = sub = 0 while 1: if i<=l-5: if s[i]=='h': i += 1 if s[i]=='e': i += 1 if s[i]=='a': i += 1 if s[i]=='v': i += 1 if s[i]=='y': i += 1 length += 1 elif s[i]=='m': i += 1 if s[i]=='e': i += 1 if s[i]=='t': i += 1 if s[i]=='a': i += 1 if s[i]=='l': i += 1 sub += length else: i += 1 else: break sys.stdout.write(str(sub))
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() heavy = 0 answer = 0 for i in range(len(str) - 4): if (str[i:i+5] == "heavy"): heavy+=1 elif (str[i:i+5] == "metal"): answer += heavy print answer
PYTHON