Programing/Algorithm

poj 2608 Soundex

sosal 2014. 5. 6. 07:32
반응형

/*
 http://sosal.tistory.com/
 * made by so_Sal
 */


Description


Soundex coding groups together words that appear to sound alike based on their spelling. For example, "can" and "khawn", "con" and "gone" would be equivalent under Soundex coding. 

Soundex coding involves translating each word into a series of digits in which each digit represents a letter: 

      1 represents B, F, P, or V


      2 represents C, G, J, K, Q, S, X,  or Z


      3 represents D or T


      4 represents L


      5 represents M or N


      6 represents R


The letters A, E, I, O, U, H, W, and Y are not represented in Soundex coding, and repeated letters with the same code digit are represented by a single instance of that digit. Words with the same Soundex coding are considered equivalent.

Input


Each line of input contains a single word, all upper case, less than 20 letters long.

Output


For each line of input, produce a line of output giving the Soundex code. 

Sample Input


KHAWN

PFISTER

BOBBY


Sample Output


25

1236

11

Source



입력이 EOF(end of input)로 들어오는 문제입니다.

따라서 main에서 입력은


while( !cin.eof() ){

~

}

을 이용하여 eof가 올때까지 입력받도록 프로그램을 짜셔야 합니다.


중복되는 숫자가 연속적인 경우에는 출력해선 안되므로

last 라는 tmp 변수를 둬서 이전에 출력한 값과 동일한지 확인하는 조건문을 추가하였습니다.

물론 blank가 오는 경우에는 last를 다시 초기화해줘야합니다.


case 1~6에 대한 하드코딩이 필요한 문제라 썩 풀고싶진 않은 문젠데

어렵지 않아 뚝딱 만들었습니다.






#include<iostream> #include<string> #include<algorithm> #include<vector> using namespace std; char convert(char a){ switch(a){ case 'B': case 'F': case 'P': case 'V': return '1'; case 'C': case 'G': case 'J': case 'K': case 'Q': case 'S': case 'X': case 'Z': return '2'; case 'D': case 'T': return '3'; case 'L': return '4'; case 'M': case 'N': return '5'; case 'R': return '6'; default: return 0; } } int main(){ string input; string converted; while(!cin.eof()){ input.clear(); converted.clear(); cin>>input; int last = 0; for(int i=0;i<input.size();i++){ converted+= convert(input[i]); } for(int i=0;i<converted.size();i++){ if(converted[i] == 0){ last = 0; continue; } else if( last != converted[i]) { cout<<converted[i]; last = converted[i]; } } cout<<endl; } }


'Programing > Algorithm' 카테고리의 다른 글

poj 2140 Herd Sums  (0) 2014.05.06
algospot - HAMMINGCODE  (0) 2014.05.06
algospot - Divisibility  (0) 2014.05.06
poj 2105 IP Adress  (0) 2014.04.30
acmicpc 수찾기: 1920  (0) 2014.04.30