Header Ad

HackerEarth The Alphabet Chocolate problem solution

In this HackerEarth The Alphabet Chocolate problem solution A long chocolate was given as a gift to Alice by Bob. The chocolate is a series of square tiles connected to each other in a straight line. Each chocolate piece contains an alphabet carved on it.

Now Alice picks a single part of chocolate and eats it. The taste of a chocolate's part is determined by how many squares are there in that part on which a vowel is carved.

Since there are lots of ways for Alice to choose her chocolate part, find the sum of tastes of all the possible chocolate parts that can be cut out of the given chocolate.


HackerEarth The Alphabet Chocolate problem solution


HackerEarth The Alphabet Chocolate problem solution.

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define si(x) scanf("%d",&x)
#define pi(x) printf("%d\n",x)
#define s(x) scanf("%lld",&x)
#define p(x) printf("%lld\n",x)
#define sc(x) scanf("%s",x)
#define pc(x) printf("%s",x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define inf 1e18
#define prec(x) fixed<<setprecision(15)<<x
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define mem(x,y) memset(x,y,sizeof(x))
#define PQG priority_queue< int,std::vector<int>,std::greater<int> >
#define PQL priority_queue< int,std::vector<int>,std::less<int> >
#define PQPL priority_queue<pii ,vector< pii >, less< pii > >
#define PQPG priority_queue<pii ,vector< pii >, greater< pii > >
#define PQPGB priority_queue<pii ,vector< pll >, greater< pll > >
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

using namespace std;

bool isvowel(char c) {
return (c=='a' || c=='e' || c=='i' || c=='o' || c=='u'
|| c=='A' || c=='E' || c=='I' || c=='O' || c=='U');
}

int main() {
#ifndef ONLINE_JUDGE
freopen("inp.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
fast_io;
int t; cin>>t;
assert(t>=1 && t<=10);
while(t--) {
string s; cin>>s;
int n=s.size();
assert(n>=1 && n<=100000);
for(int i=0;i<n;i++) {
assert((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'));
}
ll ans=0;
for(int i=0;i<n;i++) {
ll val=1LL*(i+1)*(n-i);
if(isvowel(s[i])) ans+=val;
}
cout<<ans<<endl;
}

return 0;
}


Second solution

 #include <bits/stdc++.h>
using namespace std;

int main() {
int t; cin>>t;
while(t--){
string s;
cin>>s;
int arr[s.length()+1];
memset(arr,0,sizeof(arr));
for(int i=0;i<s.length();i++){
if(s[i]=='a' || s[i]=='e' || s[i]=='o' || s[i]=='i' || s[i]=='u') arr[i]=1;
else if(s[i]=='A' || s[i]=='E' || s[i]=='O' || s[i]=='I' || s[i]=='U') arr[i]=1;
}
long long sum=0;
for(int i=0;i<s.length();i++){
if(arr[i]==1) sum+=((s.length()-i)*(i+1));
}
cout<<sum<<"\n";

}
return 0;
}

Post a Comment

0 Comments