알고리즘 풀이
크로아티아 알파벳
남생이야
2024. 7. 29. 15:04
https://www.acmicpc.net/problem/2941
풀이 과정
=> 처음에 크로아티아 알파벳을 Vector에 담아서 해당 문자열들을 순회해서 find 함수를 이용해 erase로 지우고 지울 때마다 카운트를 올린 다음 나중에 문자열 길이를 더하려 했다.
그러나 이 과정에서 지우는 와중에 문자열이 결합되면서 예정에 없던 크로아티아 문자열이 생겨버리고 이를 다시 지워서 예상 값보다 작아지는 문제가 발생했다. 그렇게 고민하다 결국 특수한 문자열로 치환하기로 결정하였다. 어차피 크로아티아 문자를 변환하면 한 글자로 취급하니 별 문제 없을 것이라 생각하고 다음과 같이풀어냈다.
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cctype>
#include <vector>
using namespace std;
int main()
{
std::vector<string> clist =
{
"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z=",
};
string str;
cin >> str;
int count = 0;
for (int i = 0; i < clist.size(); i++)
{
int index = 0;
while (str.find(clist[i], index) != string::npos)
{
if ((index = str.find(clist[i], index)) != string::npos)
{
/*cout << "검사한 문자열 " << clist[i] << endl;
cout << index << " find" << endl;*/
str.replace(index, clist[i].length(), "1");
}
}
}
count += str.length();
cout << count;
return 0;
}