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

UVa10260 Soundex

解法は自明だが prev = 0 の位置を while の外にしていてバグ取りに時間がかかったなどとは言えない。

#include <bits/stdc++.h>

using namespace std;

int main() {
  map<char, int> mp;
  for(char i='A'; i<='Z'; i++) mp[i] = 0;
  mp['B'] = mp['F'] = mp['P'] = mp['V'] = 1;
  mp['C'] = mp['G'] = mp['J'] = mp['K'] = mp['Q'] = mp['S'] = mp['X'] = mp['Z'] = 2;
  mp['D'] = mp['T'] = 3;
  mp['L'] = 4;
  mp['M'] = mp['N'] = 5;
  mp['R'] = 6;
  
  string s;
  while(cin >> s) {
    int prev = 0;
    for(int i=0; i<(int)s.size(); i++) {
      if(prev == mp[s[i]]) { continue; }
      if(mp[s[i]]) { cout << mp[s[i]]; }
      prev = mp[s[i]];
    }
    cout << endl;
  }
  
  return 0;
}