2013年8月19日

UVa 739 - Soundex Indexing

#include <cstdio>

int main() {
  int v[99] = {};
  v['B'] = v['P'] = v['F'] = v['V'] = 1;
  v['C'] = v['S'] = v['K'] = v['G'] = v['J'] = v['Q'] = v['X'] = v['Z'] = 2;
  v['D'] = v['T'] = 3;
  v['L'] = 4;
  v['M'] = v['N'] = 5;
  v['R'] = 6;
  printf("%9s%-25s%s\n", "", "NAME", "SOUNDEX CODE");
  char s[99];
  while (gets(s)) {
    int digit = 0;
    printf("%9s%-25s", "", s);
    putchar(s[0]);
    for (int i = 1; s[i] && digit < 3; i++) {
      if (v[s[i]] && v[s[i]] != v[s[i - 1]]) {
        printf("%d", v[s[i]]);
        digit++;
      }
    }
    while (digit < 3) {
      printf("0");
      digit++;
    }
    puts("");
  }
  printf("%19s%s\n", "", "END OF OUTPUT");
  return 0;
}

沒有留言:

張貼留言