#UVa:10260-Soundex

灆洢 2015-01-14 23:21:08

照著題目要求的進行編碼即可。

C++(0.022)

/*******************************************************/
/* UVa 10260 Soundex                                   */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2015/01/14                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

int main(){
  const int soundexCoding[] = {0, 1, 2, 3, 0, 1, 2,
                               0, 0, 2, 2, 4, 5, 5,
                               0, 1, 2, 6, 2, 3, 0,
                               1, 0, 2, 0, 2 };

  string word;
  while( cin >> word ){
    if( soundexCoding[(int)(word[0]-'A')] > 0 ){
      printf("%d", soundexCoding[(int)(word[0]-'A')]);
    }

    for( int i = 1 ; i < word.length() ; ++i ){
      if( soundexCoding[(int)(word[i]-'A')] > 0 &&
          soundexCoding[(int)(word[i]-'A')] != soundexCoding[(int)(word[i-1]-'A')] ){
        printf("%d", soundexCoding[(int)(word[i]-'A')]);
      }
    }

    printf("\n");
  }
  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料