Problem:
問題 3
ある工場では，各営業所から製品生産の注文を受けている．
前日の注文をまとめて，各製品の生産合計を求めたい．
入力ファイルの１行目には注文データの数 n が書いてあり，
続く n 行には製品名と注文数が空白で区切られて書いてある．
製品名は5文字以内の英大文字で書かれている．
注文データには同じ製品が含まれていることもあり，順序はバラバラである．
この注文データの中に現れる同じ製品の注文数を合計し，
出力ファイルに製品名と合計を空白を区切り文字として出力しなさい．
ただし，製品名に次の順序を付けて，その順で出力すること．
順序：文字の長さの小さい順に，同じ長さのときは，前から比べて
最初に異なる文字のアルファベット順とする．
入力データにおける製品数，注文数とその合計のどれも10
6
以下である．
出力ファイルにおいては，
出力の最後の行にも改行コードを入れること．
入出力例
入力例1
5
A 20
B 20
A 20
AB 10
Z 10
出力例1
A 40
B 20
Z 10
AB 10
入力例2
5
AAA 20
ABA 20
AAA 20
AAB 20
AAA 20
出力例2
AAA 60
AAB 20
ABA 20
問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include<iostream>
#include<string>
using namespace std;

int main()
{
    int n;

    cin >> n;

    string s[n];
    int p[n];

    for( int i = 0; i < n; i++ )
        cin >> s[i] >> p[i];

    for( int i = 0; i < n - 1; i++ )                //同じ製品をまとめる
        for( int j = i + 1; j < n; j++ )
            if( s[i] == s[j] )
            {
                s[j] = "";
                p[i] += p[j];
                p[j] = 0;
            }

    for( int i = 0; i < n - 1; i++ )                //文字数順に並べる。
        for( int j = i + 1; j < n; j++ )
            if( s[i].size() > s[j].size() )
            {
                string t1 = s[i];
                s[i] = s[j];
                s[j] = t1;
                
                int t2 = p[i];
                p[i] = p[j];
                p[j] = t2;
            }

    for( int i = 0; i < n - 1; i++ )                //辞書順に並べる。
        for( int j = i + 1; j < n; j++ )
            if( s[i].size() == s[j].size() && s[i] > s[j] )
            {
                string t1 = s[i];
                s[i] = s[j];
                s[j] = t1;
                
                int t2 = p[i];
                p[i] = p[j];
                p[j] = t2;
            }

    for( int i = 0; i < n; i++ )
        if( s[i] != "" )
            cout << s[i] << " " << p[i] << endl;

    return 0;
}
accuracy: 1
time_ms: 680
memory_kb: 3732
final_rank: 19

code_snippet: #include<iostream>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;

int k[16000000];
int n, a, z, cnt;
string S;

int main() {
	memset(k, 0, sizeof(k));
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> S;
		z = 0;
		for (int j = 0; j < S.size(); j++) {
			z += (int)(S[j] - 'A' + 1)*pow(27, S.size() - j - 1);
		}
		cin >> a;
		k[z] += a;
	}
	for (int i = 0; i < 16000000; i++) {
		if (k[i] >= 1) {
			for (int j = 4; j >= 0; j--) {
				z = (int)(i / (int)pow(27, j)) % 27;
				if (z >= 1) {
					cout << (char)(z + 'A' - 1);
				}
			}
			cout << ' ';
			cout << k[i] << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 63748
final_rank: 18

code_snippet: #include<iostream>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;

int k[16000000];
int n, a, z, cnt;
string S;

int main() {
	memset(k, 0, sizeof(k));
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> S;
		z = 0;
		for (int j = 0; j < S.size(); j++) {
			z += (int)(S[j] - 'A' + 1)*pow(27, S.size() - j - 1);
		}
		cin >> a;
		k[z] += a;
	}
	for (int i = 0; i < 16000000; i++) {
		if (k[i] >= 1) {
			for (int j = 4; j >= 0; j--) {
				z = (int)(i / (int)pow(27, j)) % 27;
			}
			if (z >= 1) {
				cout << (char)(z + 'A' - 1);
			}
			cout << ' ';
			cout << k[i] << endl;
		}
	}
	return 0;
}
accuracy: 0.2
time_ms: 10
memory_kb: 63748
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const ull B=100000007;
const int INF=1e9;
bool com(string l,string r){
      if(l.length()!=r.length())return l.length()<r.length();
      REP(i,l.length())if(l[i]!=r[i])return l[i]<r[i];
}
int main(){
      int n;
      map<string,int> M;
      cin>>n;
      vector<string> V;
      REP(i,n){
            string s;int a;
            cin>>s>>a;
            if(M[s]==0)V.PB(s);
            M[s]+=a;
      }
      sort(V.begin(),V.end(),com);
      REP(i,V.size()){
            cout<<V[i]<<" "<<M[V[i]]<<endl;
      }
}
accuracy: 1
time_ms: 10
memory_kb: 1232
final_rank: 7

code_snippet: #include <cstdio>
#include <stack>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(){
    int n,b;
    map<string,int> mm[5];
    string a;
    
    
    scanf("%d",&n);
    
    for(int i=0;i<n;i++){
        cin >> a >> b;
        
        if(mm[a.size()-1].find(a)==mm[a.size()-1].end()){
            mm[a.size()-1][a]=b;
        }else{
            mm[a.size()-1][a]+=b;
        }
    }
    
    for(int i=0;i<5;i++)
        for(map<string,int>::iterator it=mm[i].begin();it!=mm[i].end();it++){
        cout << it->first << " " << it->second << endl;
        }
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1272
final_rank: 13

code_snippet: /*
    これって模擬本戦...？
 */

#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int n;
map<string, int> products;
vector<string> names;

bool cmp(const string &a, const string &b) {
    if (a.length() != b.length()) return a.length() < b.length();
    return a < b;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        char buf[6];
        int num;
        string name;

        scanf(" %s", buf);
        scanf("%d", &num);
        name = string(buf);

        if (products.find(name) != products.end()) {
            products[name] += num;
        } else {
            products[name] = num;
        }

        names.push_back(name);
    }

    sort(names.begin(), names.end(), cmp);
    names.erase(unique(names.begin(), names.end()), names.end());

    for (int i=0; i<names.size(); i++) {
        printf("%s %d\n", names[i].c_str(), products[names[i]]);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 1564
final_rank: 16

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>

using namespace std;

bool comp(const string& key1, const string& key2)
{
  if(key1.size() == key2.size()) {
    return key1 < key2;
  }
  return key1.size() < key2.size();
}

int main()
{
  int n;
  cin >> n;

  string key;
  int value;
  map<string, int> products;
  vector<string> keys;

  for(int i = 0; i < n; i+= 1){
    cin >> key >> value;

    if(products.find(key) == products.end()) {
      products[key] = value;
      keys.push_back(key);
    } else {
      products[key] += value;
    }
  }

  sort(keys.begin(), keys.end(), comp);

  for(int i = 0; i < keys.size(); i += 1){
    cout << keys[i] << " " << products[keys[i]] << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1228
final_rank: 4

code_snippet: #include <string> 
#include <algorithm> 
#include <cfloat> 
#include <climits> 
#include <cmath> 
#include <complex> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <functional> 
#include <iostream> 
#include <map> 
#include <memory> 
#include <queue> 
#include <set> 
#include <sstream> 
#include <stack> 
#include <utility> 
#include <vector> 

#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(),(x).end() 
typedef long long ll;
using namespace std; 
const double eps = 1e-10;

bool func(const string &a, const string &b){
    if(a.size() != b.size()) return a.size() < b.size();
    else return a < b;
}
int main(){
    int n;
    cin >> n;
    map<string, int> mp;
    vector<string> v;
    for(int i = 0; i<n; ++i){
        string s;
        int a;
        cin >> s >> a;
        if(mp.find(s) == mp.end()){
            v.push_back(s);
        }
        mp[s] += a;
    }
    sort(ALL(v), func);
    EACH(i, v) cout << *i << " " << mp[*i] << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1228
final_rank: 4

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb(in,tmp) in.push_back(tmp)
#define all(in) in.begin(),in.end()
#define PI acos(-1)
using namespace std;
int main(){
  map<int,map<string,int> >in;
  int n;
  cin>>n;
  rep(i,n){
    int tmp;
    string tmp1;
    cin>>tmp1>>tmp;
    in[tmp1.size()][tmp1]+=tmp;
  }
  map<int,map<string,int> >::iterator ite=in.begin();
  for(;ite!=in.end();ite++){
    map<string,int>::iterator it=in[ite->first].begin();
    for(;it!=in[ite->first].end();it++)
      cout<<it->first<<" "<<it->second<<endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1228
final_rank: 4

code_snippet: #include <iostream>
#include <map>
#include <cstring>
#include <vector>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<string,int> P;

map<string, int> m;
map<string, int>::iterator it;
vector<P> ans;
int n;

int main(){
	cin >> n;
	rep(i,n){
		string str;
		int cnt;
		cin >> str >> cnt;
		m[str] += cnt;
	}
	for(int i = 1; i <= 5; i++){
		for(it = m.begin(); it != m.end(); it++){
			if((*it).first.size() == i) ans.push_back(P((*it).first,(*it).second));
		}
	}
	rep(i,ans.size()){
		cout << ans[i].first << " " << ans[i].second << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1232
final_rank: 7

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n, a;
string s;

bool cmp(const pair<string, int> &a, const pair<string, int> &b){
  if(a.F.size() != b.F.size()) return a.F.size() < b.F.size();
  return a.F < b.F;
}

int main(){
  scanf("%d", &n);
  vector<pair<string, int> >v;
  rep(i, n){
    cin >> s >> a;
    int p = 0;
    while(v.size() > p){
      if(v[p].F == s){ v[p].S += a; break; }
      p++;
    }
    if(v.size() == p) v.PB(MP(s, a));
  }
  sort(v.begin(), v.end(), cmp);

  rep(i, v.size()) cout << v[i].F << ' ' << v[i].S << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1272
final_rank: 13

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(),x.end()
#define mp make_pair
#define pb push_back

using namespace std;

bool pairCompare(const std::pair<string, int>& firstElem, const std::pair<string, int>& secondElem) {
    if(firstElem.second != secondElem.second){
        return firstElem.second > secondElem.second;
    }else{
        if(firstElem.first.size() != secondElem.first.size()){
            return firstElem.first.size() < secondElem.first.size();
        }
        else
            return firstElem.first < secondElem.first;
    }
}

int main(){
    int n;
    map<string, int> m;
    int stringSize = 0;

    cin >> n;
    for(int i=0; i < n; i++){
        string s; int p;
        cin >> s >> p;
        stringSize = max(stringSize, (int)s.size());
        m[s] += p;
    }

    for(int i=0; i <= stringSize; i++){
        map<string, int>::iterator it = m.begin();
        while(it != m.end()){
            if(i == (*it).first.size())
                cout << (*it).first << " " << (*it).second << endl;
            ++it;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 1224
final_rank: 1

code_snippet: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<string, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())

class C{
  public:
    string first;
    int second;
    C(string _f, int _s){ first = _f; second = _s; }
    C(){}
    bool operator < (const C &c) const {
      if (first.length() < c.first.length()) return true;
      if (first.length() > c.first.length()) return false;
      return first < c.first;
    }
};

int main() {
  int N; cin >>N;
  map<string, int> m;
  REP(i, N){
    string s;
    int num;
    cin >>s >>num;
    m[s] += num;
  }
  vector<C> v;
  for(map<string, int>::iterator it = m.begin(); it != m.end(); ++it) v.push_back( C(it->first, it->second) );
  SORT(v);
  REP(i, v.size()) cout <<v[i].first <<" " <<v[i].second <<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1236
final_rank: 11

code_snippet: #include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
  int n;
  cin >> n;
  int num[n],pos = 0;
  string s[n];
  for(int t=0;t<n;t++){
    cin >> s[pos] >> num[pos];
    pos++;
    for(int tt=0;tt<pos-1;tt++){
      //cout << s[tt] << " " << s[pos-1] << endl;
      if(s[tt] == s[pos-1]){ 
	num[tt] += num[pos-1];
	pos--;
	break;
      }
    }

  }

  for(int i=0;i<pos;i++){
    for(int j=pos-1;j>i;j--){
      if(s[j].size()<s[j-1].size() || (s[j].size()==s[j-1].size() && s[j]<s[j-1])){
	swap(s[j],s[j-1]);
	swap(num[j],num[j-1]);
      }
    }
  }
  
  for(int i=0;i<pos;i++){
    cout << s[i] << " " << num[i] << endl;
  }

}
accuracy: 1
time_ms: 10
memory_kb: 1296
final_rank: 15

code_snippet: #include <iostream>
#include <string>
#include <map>

using namespace std;
typedef pair<int, string> p;

void solve()
{
	int n;
	while(cin >> n)
	{
		map<p, int> list;
		for(int i = 0; i < n; ++i)
		{
			string product;
			cin >> product;
			int num;
			cin >> num;
			list[p(product.size(), product)] += num;
		}
		map<p, int>::iterator it = list.begin();
		while(it != list.end())
		{
			cout << it->first.second << " " << it->second << endl;
			++it;
		}
	}
}

int main()
{
	solve();
	return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 1224
final_rank: 1

code_snippet: #include<iostream>
#include<string>
#include<map>
using namespace std;
int main(){
  int n,ss,max=0;
  cin >> n;
  string s;
  map<string,int> m;
  map<string,int>::iterator p;
  for(int i=0;i<n;i++){
    cin >> s >> ss;
    m[s] += ss;
    if(s.size()>max) max = s.size();
  }
  for(int i=1;i<=max;i++){
    for(p=m.begin();p!=m.end();p++){
      if(p->first.size()==i) cout << p->first << ' ' << p->second << endl; 
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1224
final_rank: 1

code_snippet: /* AOJ 0589 Production */

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

bool compare_product_name(string a, string b);
void map_test();

typedef bool (*pname_comparator)(string, string);
typedef map<string, int, pname_comparator> mapp;

int
main()
{
    int n;
    mapp production(compare_product_name);
    mapp::iterator it;

    cin >> n;
    cin.ignore();
    while (n--) {
        string line, name;
        int p;
        int delim_pos;

        getline(cin, line);

        delim_pos = line.find_first_of(' ');
        name = line.substr(0, delim_pos);
        p = atoi(line.substr(delim_pos + 1).data());

        if (production.find(name) == production.end())
            production[name] = p;
        else
            production[name] += p;
    }

    for (it = production.begin(); it != production.end(); ++it)
        cout << it->first << ' ' << it->second << endl;

    return 0;
}

bool
compare_product_name(string a, string b)
{
    size_t len_a = a.size();
    size_t len_b = b.size();
    size_t min_len = min(len_a, len_b);
    size_t i;

    if (len_a != len_b)
        return len_a < len_b;

    for (i = 0; i < min_len; i++) {
        if (a[i] != b[i])
            return a[i] < b[i];
    }

    // a == b なので a < b ではない
    return false;
}
accuracy: 1
time_ms: 20
memory_kb: 1236
final_rank: 17

code_snippet: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int n;
map<string,int> m;
vector<pair<pair<int,string>,int> > v;
int main(void){
  cin >> n;
  for(int i = 0; i < n; i++){
    string s;
    int a;
    cin >> s >> a;
    if(m.find(s) == m.end()){
      m[s] = v.size();
      v.push_back(make_pair(make_pair(s.size(),s),-a));
    }else{
      v[m[s]].second -= a;
    }
  }
  sort(v.begin(),v.end());
  for(int i = 0; i < (int)v.size(); i++){
    cout << v[i].first.second << " " << -v[i].second << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1232
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <iterator>
using namespace std;

int main() {
    int N; cin >> N;
    map<string, int> dict;
    while(N--) {
        string s; int x; cin >> s >> x;
        dict[s] += x;
    }
    vector<pair<string, int> > v;
    for(auto it : dict) {
        v.push_back(it);
    }
    sort(begin(v), end(v), [](pair<string, int> a, pair<string, int> b) {
            if(a.first.length() != b.first.length()) return a.first.length() < b.first.length();
            else return a.first < b.first;
            });
    for(auto it : v) {
        cout << it.first << ' ' << it.second << endl;
    }

}
accuracy: 1
time_ms: 10
memory_kb: 1240
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>

struct Product{
	std::string name;
	int amount;
};

int main(){
	std::vector<Product> v;

	int n;
	std::cin >> n;
	for(int i=0;i<n;i++){
		std::string name;
		int amount;
		std::cin >> name >> amount;
		auto it = std::find_if(v.begin(), v.end(), [&name](Product p){
					return p.name == name;
			});
		if(it == v.end()){
			v.push_back({name, amount});
		}else{
			it->amount += amount;
		}
	}

	std::sort(v.begin(), v.end(), [](Product lp, Product rp){
			if(lp.name.size() != rp.name.size())return lp.name.size() < rp.name.size();
			for(int i=0;i<lp.name.size();i++){
				if(lp.name[i] != rp.name[i])return lp.name[i] < rp.name[i];
			}
		});

	for(auto p : v){
		std::cout << p.name << " " << p.amount << std::endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1232
final_rank: 7