Author: Akshat Agrawal
Tester: Yash Tiwari
Editorial: Shivendra Tripathi
Difficulty:
Easy
Prerequisite:
Array , Hashing
Explanation & Algorithm:
In the question, you have given a T test case in each case you have given N name but the twist is that you haven’t given how many names are present in a list. To take the whole line we can use the getline function in C++/ nextLine() in Java.
Then iterate through whole line store name in vector in C++/ ArrayList in Java by breaking it in space (ASCII 32)
Now by using unordered_map in C++ / Hashtable in Java (to store strings with their frequency ).
Now to print names with their corresponding frequency
We use unordered_map in C++ / Hashtable in Java .
Solution
Setter's Code
#include <bits/stdc++.h>
using namespace std ;
#define SUKUNA ios_base :: sync_with_stdio (false); cin . tie ( 0 ); cout . tie ( 0 )
#define int long long
void solve () {
string p ;
getline ( cin , p );
p += ’ ’ ;
string c = “” ;
vector < string > vv ;
for ( int i = 0 ; i < p . size (); i ++){
if ( p [ i ] == 32 ){
vv . push_back ( c );
c = “” ;
}
else {
c += p [ i ] ;
}
}
unordered_map < string , int > mp1 ;
unordered_map < string , int > mp2 ;
for ( auto i : vv ){
mp1 [ i ] ++;
mp2 [ i ] = 1 ;
}
cout << vv . size () << " \n " ;
for ( int i = 0 ; i < vv . size (); i ++){
if ( mp1 [ vv [ i ]] > 1 ){
cout << vv [ i ]<< " " << mp2 [ vv [ i ]]<< " \n " ;
mp2 [ vv [ i ]] ++;
}
else {
cout << vv [ i ]<< " \n " ;
}
}
}
signed main () {
SUKUNA ;
int test = 1 ;
cin >> test ;
cin . ignore ();
while ( test –){
solve ();
}
return 0 ;
}
Tester's Code
t=int(input())
for i in range(t):
arr=input().split()
h={}
for i in arr:
if i not in h:
h[i]=[1,0]
else:
h[i][0]+=1
print(len(arr))
for name in arr:
if name in h:
h[name][0]-=1
h[name][1]+=1
if h[name][0]==0 and h[name][1]==1:
print(name)
else:
print(name+" "+str(h[name][1]))