PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Practice
Setter: Anshul Garg
Tester & Editorialist: Taranpreet Singh
DIFFICULTY
Cakewalk
PREREQUISITES
None
PROBLEM
Given N words consisting of Uppercase English characters and two integers A and B, two players Sarthak and Anuradha play a game. For each word, if the first character of the word appears in string “EQUINOX”, Sarthak gets A points, otherwise Anuradha gets B points. The person with higher score wins.
QUICK EXPLANATION
- Simulate the process, maintaining the scores and compare them.
EXPLANATION
Since this is an easier problem, all we need to do is to simulate the game. Start with scores for both players being zero and consider word one by one.
To check if first characters appears in string EQUINOX, one naive way would be to write 7 if conditions. It works, but too much work for nothing. We can simplify it.
One way would be to us would be to create a string EQUINOX and use the inbuild find function to check whether the character appears in string EQUINOX or not.
Another way would be to make an array or a set.
Following code snippet depict the intended solution
string EQUINOX = "EQUINOX"
SarthakScore = 0
AnuradhaScore = 0
for word in wordList:
if word[0] in EQUINOX:
SarthakScore += A
else:
AnuradhaScore += B
Don’t forget to use long data type, since the scores can easily go beyond 32 bit integer range.
TIME COMPLEXITY
The time complexity is O(\sum |S_i|) per test case, since we read that as input.
SOLUTIONS
Setter's Solution
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
#define int long long
string tt = "EQUINOX";
void solve(){
int n, a, b;
int ans1 = 0, ans2 = 0; cin >> n >> a >> b;
for(int i = 0; i < n; i++){
string s; cin >> s;
int f = 0;
for(int j = 0; j < tt.size(); j++){
if(tt[j] == s[0]) f = 1;
}
if(f) ans1 += a;
else ans2 += b;
}
if(ans1 == ans2) cout << "Draw\n";
else if(ans1 > ans2) cout << "Sarthak\n";
else cout << "Anuradha\n";
}
signed main(){
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t;
cin >> t;
while(t--) solve();
}
Tester's Solution
import java.util.*;
import java.io.*;
class EQUINOX{
//SOLUTION BEGIN
void pre() throws Exception{}
void solve(int TC) throws Exception{
int N = ni();
long A = nl(), B = nl();
long sarthak = 0, anuradha = 0;
String pattern = "EQUINOX";
for(int i = 0; i< N; i++){
String word = n();
if(pattern.contains(word.substring(0,1)))sarthak += A;
else anuradha += B;
}
if(sarthak > anuradha)pn("SARtHak");
else if(sarthak < anuradha)pn("aNUrAdHA");
else pn("draW");
}
//SOLUTION END
void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
static boolean multipleTC = true;
FastReader in;PrintWriter out;
void run() throws Exception{
in = new FastReader();
out = new PrintWriter(System.out);
//Solution Credits: Taranpreet Singh
int T = (multipleTC)?ni():1;
pre();for(int t = 1; t<= T; t++)solve(t);
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
new EQUINOX().run();
}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
String n()throws Exception{return in.next();}
String nln()throws Exception{return in.nextLine();}
int ni()throws Exception{return Integer.parseInt(in.next());}
long nl()throws Exception{return Long.parseLong(in.next());}
double nd()throws Exception{return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception{
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception{
String str = "";
try{
str = br.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
}
Feel free to share your approach. Suggestions are welcomed as always.