Official

B - 459 Editorial by en_translator


As instructed in the problem statement, read each string \(S_i\), find \(C_i\) according to its first character, and concatenate them.
There are several ways to find the mapping from the first character of \(S_i\) to \(C_i\); using an array or a string may make it easy. One may also stick to if statements, although the implementation might bloat up.
In any case, the time complexity is small enough.

Sample code in C++:

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

int main(void){
	string StoC="22233344455566677778889999";
	int n;
	string s,ans;

	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s;
		ans+=StoC[s[0]-'a'];
	}
	cout<<ans<<endl;
	
	return 0;
}

Sample code in C++ 2:

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

int main(void){
	
	string StoC="";
	for(int i=2;i<=9;i++){
		for(int j=0;j<3;j++)StoC+=(char)('0'+i); //3 characters for 2 3 4 5 6   8
		if((i==7)||(i==9))StoC+=(char)('0'+i);   //4 characters for           7   9
	}

	int n;
	string s,ans;

	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s;
		ans+=StoC[s[0]-'a'];
	}
	cout<<ans<<endl;
	
	return 0;
}

Sample code in Python:

StoC="22233344455566677778889999"
ans=""

n=int(input())
s=list(input().split())
for i in range(n):
    ans+=StoC[ord(s[i][0]) - ord('a')]

print(ans)

posted:
last update: