Header Ad

HackerEarth Acronym problem solution

In this HackerEarth Acronym problem solution 

W.T.H.S.E.C

Confused? Well, it stands for Welcome To HackerEarth September Easy Challenge :). It's quite amazing when long words like this get reduced to such a short string. They sound pretty cool and often make the chat interesting. They are almost used everywhere these days.

Our friend Harsh has been recently reducing every sentence he receives to an acronym. Since Harsh is a young lad he has his own personal likes and dislikes. There are some words which he just does not want to see in a sentence. So whenever he encounters such a word he simply ignores it. Harsh is thinking of converting a book to an acronym book where each sentence of the book is reduced to its short form. Since Harsh is quite busy promoting his new book he wants you to help him in doing so.

So he gives you a set of words that he dislikes and a sentence for which he wants to abbreviate. Help him in getting the required abbreviation.


HackerEarth Acronym problem solution


HackerEarth Acronym problem solution.

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define all(a) a.begin(),a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define MOD 10009
#define MAXN 100005
typedef unsigned long long int uint64;
typedef long long int int64;

map<string,int>dislike;

int main(){
int k,i;
string s;
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
cin>>k;
while(k--){
cin>>s;
dislike[s]=1;
}
scanf("\n");
getline(cin,s);
s+=" ";
string ans="";
string ret="";

for(i=0;i<s.length();i++){
if(s[i]==' '){
if(ret.empty())
continue;

if(dislike.count(ret)){
ret="";
continue;
}
ans+=toupper(ret[0]);
ret="";
}
else{
ret+=s[i];
}
}
cout<<ans[0];
for(i=1;i<ans.length();i++){
cout<<"."<<ans[i];
}
fclose(stdout);
return 0;
}


Second solution

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <assert.h>
#include <queue>
using namespace std;

int n;
string bad[11];
string hh;
int m;
char sol[111];
int r=0;
int main(){
cin>>n;
assert(1<=n && n<=10);
for(int i=0;i<n;i++){
cin>>bad[i];
assert(bad[i].length()<=100);
}
cin>>m;
int y=m;
int l=0;
while(y--){
cin>>hh;
l+=hh.length();
bool found=false;
for(int i=0;i<n;i++){
if(hh==bad[i]){
found=true;
break;
}
}
if(!found){
sol[r++]=(char)(hh[0]-'a'+'A');
}
}
assert(l+m-1<=100);
string g;
if(cin>>g)
assert(false);
cout<<sol[0];
assert(r!=0);
for(int i=1;i<r;i++){
cout<<'.'<<sol[i];
}
cout<<endl;
}

Post a Comment

0 Comments