This post is regarding this problem: http://www.spoj.com/problems/GIFT1/
The following code gets AC:
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cassert>
typedef std::string str;
using std::vector;
using std::cin;
using std::cout;
void read(str& s){
getline(cin,s);
}
void read(int& n){
str s;
getline(cin,s);
std::stringstream ss(s);
ss >> n;
}
void read(int& n1, int &n2){
str s;
getline(cin,s);
std::stringstream ss(s);
ss >> n1 >> n2;
}
int main() {
int N = 0;
vector<str> names;
std::map<str,int> bank;
read(N);
for (int i = 0; i < N; i++) {
str name;
read(name);
names.push_back(name);
bank[name] = 0;
}
// Gift amount; Number of gift givees.
int G, M;
str name;
for (int n = 0; n < N; n++) {
read(name);
cin >> G >> M;
//read(G,M);
cin >> std::ws;
// Add the modulus (leftover money from the giver).
bank[name] -= G - (M == 0? G : G % M);
for (int i = 0; i < M; i++) {
read(name);
bank[name] += G / M;
}
}
for (int i = 0; i < N; i++) {
printf("%s %d\n", names[i].c_str(), bank[names[i]]);
}
}
However the following code (identical except for change in one line) gives WA:
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cassert>
typedef std::string str;
using std::vector;
using std::cin;
using std::cout;
void read(str& s){
getline(cin,s);
}
void read(int& n){
str s;
getline(cin,s);
std::stringstream ss(s);
ss >> n;
}
void read(int& n1, int &n2){
str s;
getline(cin,s);
std::stringstream ss(s);
ss >> n1 >> n2;
}
int main() {
int N = 0;
vector<str> names;
std::map<str,int> bank;
read(N);
for (int i = 0; i < N; i++) {
str name;
read(name);
names.push_back(name);
bank[name] = 0;
}
// Gift amount; Number of gift givees.
int G, M;
str name;
for (int n = 0; n < N; n++) {
read(name);
//cin >> G >> M;
read(G,M);
cin >> std::ws;
// Add the modulus (leftover money from the giver).
bank[name] -= G - (M == 0? G : G % M);
for (int i = 0; i < M; i++) {
read(name);
bank[name] += G / M;
}
}
for (int i = 0; i < N; i++) {
printf("%s %d\n", names[i].c_str(), bank[names[i]]);
}
}
I can’t figure out why this code gives WA but the previous code gets AC. The only reason I can think of is that the test cases are in the wrong format (specifically, the amount to gift and the number of people should be on the same line, separated by one space, but they are apparently not). Could someone please enlighten me?