반응형
문제 출처 :
https://www.acmicpc.net/problem/2941
알고리즘 분석 :
문제 해결에 필요한 사항
1. String STL :: http://www.crocus.co.kr/503
String STL 기법을 이용해보기에 좋은 문제인 것 같다.
if (str.find(del) != string::npos)
{
while ((pos = str.find(del)) != string::npos)
{
str.erase(pos, del.length());
str.insert(pos, star);
}
}
이 부분의 npos에 대한 설명이다.
string::find는 찾는 문자열의 첫번째 인덱스를 반환한다. 이때 찾는 문자열이 없을 경우 string::npos를 반환한다.
결국 find를 통해 크로아티아 문자를 찾게 되고, 없으면 npos를 반환하게 된다는 의미이다.
이제 크로아티아 문자를 찾았다면, 그 위치부터 length만큼 삭제해주고,
그 부분에 "*"이라는 것을 삽입하여 크로아티아 문자를 크기 1인 문자로 강제 변환 시켜주게 된다면
str.size()가 답이 된다.
소스 코드 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <iostream> #include <cstdio> #include <string> using namespace std; string str; int main() { cin >> str; int pos = 0; string croatia[8] = { "c=","c-","dz=","d-","lj","nj","s=","z=" }; string del; string star = "*"; for (int i = 0; i < 8; i++) { del= croatia[i]; if (str.find(del) != string::npos) { while ((pos = str.find(del)) != string::npos) { str.erase(pos, del.length()); str.insert(pos, star); } } } cout << str.size() << endl; return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[11945번] 뜨거운 붕어빵 (0) | 2017.04.21 |
---|---|
[3793번] Common Subsequence (5) | 2017.04.21 |
[3943번] 헤일스톤 수열 (0) | 2017.04.21 |
[2563번] 색종이 (0) | 2017.04.21 |
[5347번] LCM (0) | 2017.04.21 |