Re: 484-The Department of Redundancy Department Getting WA..
Posted: Wed Dec 18, 2013 12:28 am
The input file may be arbitrarily long. I used this to parse the input:
while(scanf("%d", &i) == 1) {
while(scanf("%d", &i) == 1) {
The Online Judge board
https://onlinejudge.org/board/
Code: Select all
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <sstream>
#include <utility>
using namespace std;
vector<string> str_index ;
vector<string> str_index2 ;
map<string , int> str_counter ;
map<string , int> str_chk ;
int main()
{
long int i,j,k,t_case,len,len2 ;
string str,tmp ;
while(getline(cin,str))
{
str_index.clear() ;
str_counter.clear() ;
str_chk.clear() ;
str_index2.clear() ;
stringstream ss(str) ;
while(ss >> tmp)
{
str_index.push_back(tmp) ;
if(str_chk[tmp] == 0)
{
str_index2.push_back(tmp) ;
str_chk[tmp] = 1 ;
}
}
len2 = str_index2.size() ;
len = str_index.size() ;
for(i = 0 ; i < len2 ; i++)
{
tmp = str_index2[i] ;
for(j = 0 ; j < len ; j++)
{
if(str_index2[i] == str_index[j])
{
str_counter[tmp]++ ;
}
}
}
for(i = 0 ; i < len2 ; i++)
{
tmp = str_index2[i] ;
cout << str_index2[i] << " " << str_counter[tmp] << endl ;
}
}
return 0;
}
It's never a good idea to make assumptions about things that are not explicitly stated in the problem statement.cse dipto wrote:(code omitted)
Code: Select all
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <sstream>
#include <utility>
using namespace std;
vector<string> str_index ;
vector<string> str_index2 ;
map<string , int> str_counter ;
map<string , int> str_chk ;
int main()
{
long int i,j,k,t_case,len,len2 ;
string str,tmp ;
getline(cin , str) ;
stringstream ss(str) ;
while(ss >> tmp)
{
str_index.push_back(tmp) ;
if(str_chk[tmp] == 0)
{
str_index2.push_back(tmp) ;
str_chk[tmp] = 1 ;
}
}
len2 = str_index2.size() ;
len = str_index.size() ;
for(i = 0 ; i < len2 ; i++)
{
tmp = str_index2[i] ;
for(j = 0 ; j < len ; j++)
{
if(str_index2[i] == str_index[j])
{
str_counter[tmp]++ ;
}
}
}
for(i = 0 ; i < len2 ; i++)
{
tmp = str_index2[i] ;
cout << str_index2[i] << " " << str_counter[tmp] << endl ;
}
return 0 ;
}
Code: Select all
3 1 2 2 1 3 5 3 3 2
3 1 2 2 1 3 5 3 3 2
Code: Select all
3 8
1 4
2 6
5 2
My AC output on the same sample he gave was:brianfry713 wrote:Input:AC output:Code: Select all
3 1 2 2 1 3 5 3 3 2 3 1 2 2 1 3 5 3 3 2
Code: Select all
3 8 1 4 2 6 5 2
Code: Select all
3 6
1 4
2 4
5 2