티스토리 뷰
문장을 저장할 string, count를 추가할지 여부를 결정하는 bool isRight, 이미 나온 알파벳을 저장해두는 list<char>로 구성된다.
문장 내 모든 char에 대해서 아래를 반복한다 :
alreadyExisted 초기화, isRight true로 초기화 후....
alreadyExisted에 이 값이 존재하는가?
Yes : 이전 문자가 지금과 다른가? : isRight : false로 바꾸고 break
No : alreadyExisted에 추가
* index가 범위를 벗어날 경우 - alreadyExisted에 존재하고 이전 문자를 조사하는데 그게 첫번째 문자였다면 예외발생
: continue;
반복문의 마지막에 isRight를 검사하고 여부에 따라 count 변경
BufferedStream bs = new BufferedStream(Console.OpenStandardInput());
StreamReader sr = new StreamReader(bs);
string sentence;
List alreadyExisted = new List();
bool isRight = true;
int n = int.Parse(sr.ReadLine());
int count = 0;
while(n-->0)
{
alreadyExisted.Clear();
isRight = true;
sentence = sr.ReadLine();
for(int i = 0; i < sentence.Length; i++)
{
try
{
if (alreadyExisted.Contains(sentence[i]))
{
if (!sentence[i - 1].Equals(sentence[i]))
{
isRight = false;
break;
}
}
else
alreadyExisted.Add(sentence[i]);
}catch(IndexOutOfRangeException)
{
continue;
}
}
if (isRight)
count++;
}
Console.WriteLine(count);
전에 비슷하게 알파벳 관련 다루는 문제에서도 나는 List<char>를 이용했는데, 아무리 생각해도 char[26] 적용하는게 더 빠를 것 같다. 보통 지우는 연산이 포함되면 List쪽이 더 느릴테니까?
'■ 알고리즘 > ◻ 백준' 카테고리의 다른 글
| [C#/C++]백준 10809번 : 알파벳 찾기 (0) | 2018.08.14 |
|---|---|
| [C#]백준 1157번 : 단어 공부 (0) | 2018.08.14 |
| [C#]백준 2941번 : 크로아티아 알파벳 (0) | 2018.08.13 |
| [C#]백준 1032번 : 명령 프롬프트 (0) | 2018.08.13 |
| [C#]백준 2908번 : 상수 (0) | 2018.08.13 |