C++)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
bool arr[26];
int ans = 0;
int t;
cin >> t;
while (t--) {
string str;
cin >> str;
char past = '\0';
bool flag = false;
fill(arr, arr + 26, false);
for (auto ch : str) {
if (!arr[ch - 'a'] && past != ch) {
arr[ch - 'a'] = true;
past = ch;
}
else if (arr[ch - 'a'] && past != ch) {
flag = true;
break;
}
}
if (!flag)
ans += 1;
}
cout << ans;
return 0;
}
Python)
import sys
read = sys.stdin.readline
t = int(read().rstrip())
ans = 0
for _ in range(t):
st = read().rstrip()
arr = [-1] * 26
past = ''
flag = False
for ch in st:
pos = ord(ch)-ord('a')
if arr[pos] == -1 and past != ch:
arr[pos] = 1
past = ch
# 이전에 나왔던 단어인데 직전 단어와 다른 단어임
elif arr[pos] == 1 and past != ch:
flag = True
break
ans += (0 if flag else 1)
print(ans)'백준 1 > 기초' 카테고리의 다른 글
| [백준 2775] 부녀회장이 될테야 (C++/Python) (0) | 2020.12.05 |
|---|---|
| [백준 2908] 상수 (C++/Python) (0) | 2020.12.05 |
| [백준 10824] 네 수 (C++/Python) (0) | 2020.12.05 |
| [백준 10808] 알파벳 개수 (C++ / Python) (0) | 2020.12.05 |
| [백준 11655] ROT 13 (C++/Python) (0) | 2020.12.05 |