### 문제 설명 : ;
A stringssof lengthnn(1≤n≤261≤n≤26) is calledalphabeticalif it can be obtained using the following algorithm:
- first, write an empty string toss(i.e. perform the assignmentss := "");
- then perform the next stepnntimes;
- at theii-th step takeii-th lowercase letter of the Latin alphabet and write it either to the left of the stringssor to the right of the stringss(i.e. perform the assignmentss := c+sc+sorss := s+cs+c, whereccis theii-th letter of the Latin alphabet).
In other words, iterate over thennfirst letters of the Latin alphabet starting from 'a' and etc. Each time we prepend a letter to the left of the stringssor append a letter to the right of the stringss. Strings that can be obtained in that way are alphabetical.
For example, the following strings are alphabetical: "a", "ba", "ab", "bac" and "ihfcbadeg". The following stringsare notalphabetical: "z", "aa", "ca", "acb", "xyz" and "ddcba".
From the given string, determine if it is alphabetical.
틀린이유
이 문제를 처음 볼때 기준을 두고 좌우로 늘어나서 반반 잘라서 보나? 생각이 들었는데 실패이다.
다시 보는데 스택에 넣어서 기준이 되는 값을 보다 적은 것이 있으면 그 값을 지우고 기준이 되는 것을 넣고의 방식을 생각했는데
구현 하다가 실패해버렸다
문제 풀이
앞뒤가 들어갔다 나왔다 하는데 이것을 활용한 자료구조는 덱이있다.
이것들 설명을 들으니 정말 간단했다
문자열 str
마지마 값 원소 -> str.size() -1를 기준으로 원소들을 다 덱에 있을때
1) 가장 앞에 마지막 원소인지 본다
-> 가장 앞을 제거 후 마지막 값 원소도 --
2) 가장 뒤가 마지막 원소인지 본다
-> 가장 뒤를 제거 후 마지막 값 원소도 --
3) 그것이 아니면 틀린 것이다
-> flag = false
while (!d.empty())
{
if (d.front() == last) {
d.pop_front();
last--;
}
else if (d.back() == last) {
d.pop_back();
last--;
}
else {
flag = false;
break;
}
}
'코드포스 > div3' 카테고리의 다른 글
A. Shortest Path with Obstacle (0) | 2021.08.09 |
---|---|
B2. Wonderful Coloring - 2 (0) | 2021.08.05 |
B1.WonderfulColoring (2) | 2021.07.29 |