https://www.acmicpc.net/problem/1316
풀이 과정
=> 이 문제는 문자를 지워가면서 처리했다. 문자열에서 문자를 지우면 erase 함수 특성상 지운 공간을 뒤에서 당겨와 메꾸기 때문에 문자열을 역순으로 참조하여 지워갔다. map 문자를 하나씩 저장하고 사전에 검사한 문자를 저장하는 변수를 함께 이용하여 풀어냈다.
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cctype>
#include <vector>
using namespace std;
bool IsGroupWord(string& str)
{
map<char, bool> maps;
char prev = '\0';
for (int i = str.length()-1; i >=0 ; i--)
{
// 이전에 문자값과 다르다면
if (prev != str[i])
{
// 사전에 저장해놓은 것인지 검사
if (maps.find(str[i]) != maps.end())
{
// 사전에 저장해놓은 거라면
if (maps[str[i]])
{
//cout << "탈락 : " << str << endl;
return false;
}
}
else
{
prev = str[i];
//cout << "저장 : " << prev << endl;
maps[str[i]] = true;
str = str.erase(i, 1);
}
}
else
{
prev = str[i];
maps[str[i]] = true;
str = str.erase(i, 1);
}
}
return true;
}
int main()
{
int T;
cin >> T;
int count = 0;
for (int i = 0; i < T; i++)
{
string str;
cin >> str;
bool result = IsGroupWord(str);
if (result)
count++;
}
cout << count;
return 0;
}
'알고리즘 풀이' 카테고리의 다른 글
N과 M (3) (0) | 2024.08.07 |
---|---|
너의 평점은 (0) | 2024.07.30 |
크로아티아 알파벳 (0) | 2024.07.29 |
1157 단어 공부 (0) | 2024.07.29 |
2444번 별 찍기-7 (0) | 2024.07.23 |