10420 - List of Conquests(UPDATE)

在 10420 - List of Conquests 的解法中, 最后输出时为了保证国家名是字典序从小到大, 把 map 的 key 都拷贝到 vector 里来进行排序; 后来发现 map 的 key 默认就是按字典序从小到大的, 所以那段代码是多此一举;
新代码实现如下:

代码:

# include <iostream>
# include <string>
# include <cstdio>
# include <cstring>
# include <vector>
# include <algorithm>
# include <cctype>
# include <iterator>
# include <assert.h>
# include <map>
using namespace std;

// 使用 sort 排序

int main(int argc, char const *argv[])
{
  #ifndef ONLINE_JUDGE
    freopen("10420_i.txt", "r", stdin);  
    freopen("10420_o.txt", "w", stdout); 
  #endif
  
  int numLine;
  cin >> numLine;
  cin.ignore();   // cin 之后接 getline 一定要记得这一步

  string line;
  map<string, int> women;

  // 输入
  while (numLine--) {
    getline(cin, line);

    for (int i=0; i<line.size(); i++) {
      if ( !isalpha(line[i]) ) {
        string country = line.substr(0, i);
        women[country] = women[country] + 1;
        break;
      }
    }
  }

  // 输出
  for (map<string, int>::iterator it = women.begin(); it != women.end(); it++) {
    cout << it->first << " " << it->second << endl;
  }
  
  return 0;
}
环境:    C++ 4.5.3 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE


你可能感兴趣的:(list,uva,of,10420,Conquests)