Google Codejam Round 1C

In 2nd question : Code Jam - Google’s Coding Competitions

After the contest I resubmitted my code by replacing int by long long in line 33, and it gave AC. But I am unable to understand why this is happening as I have not even used that int (x). And according to me my first code should also give AC. can someone please explain why this is happening ?

my wrong code:

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

#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define si(a) scanf(“%d”,&a)
#define sl(a) scanf(“%lld”,&a)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define f(i,j,n) for(int i=j;i<n;i++)
#define ll long long
#define m1 (ll)(1e9+7)
#define m2 (ll)998244353
#define F first
#define S second
#define goo(i) cout<<“Case #”<<i<<": ";
typedef pair<int,int> pii;
typedef map<pii,int> mpi;
typedef vector vi;
typedef map<int,vi> mivi;
typedef map<int, int> mii;
typedef vector vl;
typedef vector vvi;
typedef vector vpii;

int main(){
IO;
int t,u;cin>>t;
for(int l=0;l<t;l++){
cin>>u;
int x;string s;set ss;
map<char,int> m;
for(int i=0;i<10000;i++){
cin>>x>>s;
m[s[0]]++;
int sz=s.size();
for(int r=0;r<sz;r++) ss.insert(s[r]);

	}
	goo(l+1);
	vector<pair<int,char>> pp;
	for(pair<char,int> p: m){
	    pp.pb(mp(p.second,p.first));
	}
	sort(all(pp));
	reverse(all(pp));
	string ans(10,'q');
	for(int i=1;i<10;i++) {ans[i]=pp[i-1].S;ss.erase(pp[i-1].S);}
	ans[0]=*(ss.begin());
	
	cout<<ans<<'\n';
	
	}

}

try this locally with large x (something like 1E10):

int x;string s;cin>>x>>s;
cout << s << endl;

You will see that you are not reading s correctly
'
1 Like

Oh!!! thanks a lot.
This is due to integer overflow?

yes, cin tries to assign the value according to the type, so if you check x it is equal to (1 << 31) -1 in that example, the largest int possible; and that’s it, cin stops assigning from that point without generating an error

I have been unqualified due to this. :grimacing:

if hurts…you learn! :wink:

2 Likes