백준

2902 KMP는 왜 KMP일까?

불타는강정 2019. 10. 8. 13:40
printf("%c", str[0]);

for(int i = 1; i < str.length(); i++){
    if(str[i] == 45)
       printf("%c", str[i+1]);

처음에 어짜피 맨 처음꺼를 출력하니깐 이렇게 짜야지 생각했는데, 만약 문자열이 아무것도 안 들어오면 어쩌냐 싶어서 반복문 안에 넣었다. 

 

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str;
    cin >> str;
    
    for(int i = 0; i < str.length(); i++){
        if(i == 0)
            printf("%c", str[0]);
        if(str[i] == 45)
            printf("%c", str[i+1]);
    }
}

코드는 간단하다~ 첫 문자를 출력하고 - 하이픈이 나오면 그 다음 문자를 출력한다.