2014年10月20日星期一

[UVa] 10420 - List of Conquests


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int main(){
  //freopen("input.txt","r",stdin);
  char a[76]; 
  string countries[3000];  // record countries
  string buffer; //country to compare
  char tmp[100];
  int country_count[3000]; // count the girls each country
  memset(country_count,0,sizeof(country_count));
  
  int n;
  scanf("%d",&n);
  getchar();
  for(int i=0;i<n;i++){
    fgets(a,sizeof(a),stdin); //讀取一整行
    for(int j=0;j<76;j++){  
      if(a[j]!=' ')tmp[j]=a[j];  //在第一個空白出現之前,將字元存入tmp
      else break; //讀取到空白就跳出,如此僅拿取前面的國家,後面名字不理會
    }
    countries[i] = tmp; // tmp 指定給 countries[i]
  }
  sort(countries,countries+n); //按字典排序
  
  int count=0;
  for(int i=0;i<n;i++){
    if(i==0){
      buffer = countries[i];  //一開始先將第一個國家存入暫存
    }
    if(buffer == countries[i]){ //如果接下來讀取的國家和暫存器相同
      country_count[count]++;  //計數器+1
    }
    
    if(buffer != countries[i] || i == n-1){  //如果讀取的國家和暫存不同,或是讀取到最後一個項目
      cout << buffer << " " << country_count[count] << '\n'; //輸出暫存器國家,和該國家的計數
      count++; //切換到下一個國家的計數
      buffer = countries[i];  //切換到下一個國家,存入暫存
      country_count[count]++; //新國家的計數器+1
    }
  }  
  
  
  return 0;
}

沒有留言: