読者です 読者をやめる 読者になる 読者になる

PKU 1002 487-3279

問題

(http://wikiwiki.jp/pku/?1002%20487-3279)。

解法

map使うと簡単、入力を全部数えていって、複数あるやつだけ表示させる。 cin使うとTLEになった

コード

int n;
vector<string>s;
char in[100];

int main(){
  map<char, int>m;
  m['A'] = m['B'] = m['C'] = '2'; m['D'] = m['E'] = m['F'] = '3'; m['G'] = m['H'] = m['I'] = '4';
  m['J'] = m['K'] = m['L'] = '5'; m['M'] = m['N'] = m['O'] = '6'; m['P'] = m['R'] = m['S'] = '7';
  m['T'] = m['U'] = m['V'] = '8'; m['W'] = m['X'] = m['Y'] = '9';

  scanf("%d", &n);
  s.resize(n);
  map<string, int>res;
  rep(i, n){
    scanf("%s", in);
    s[i] = string(in);
    size_t pos;
    while((pos = s[i].find_first_of("-")) != string::npos) s[i].erase(pos, 1);

    rep(j, s[i].size()){

      if(s[i][j] <= '9' && s[i][j] >= '0') continue;
      s[i][j] = m[s[i][j]];
    }
    s[i].insert(3, "-");

    if(res.count(s[i])) res[s[i]]++;
    else res[s[i]] = 1;
  }

  int f = 1;
  for(map<string, int>::iterator it = res.begin(); it != res.end(); it++){
    if(it->second <= 1) continue;
    cout << it->first;
    printf(" %d\n", it->second);
    f = 0;
  }

  if(f) puts("No duplicates.");
  return 0;
}