티스토리 뷰
문자열 S에서 T가 없을때까지 계속 지우고 S를 출력하는 문제다
처음에 그냥 string STL도 써보고 kmp도 써봤는데 시간초과났다 ㅎㅎ
100만이더라 ㅎㅎ
그래서 스택으로 풀었다
진짜 스택을 써서 풀었는데 그럼 넘 느렸다
앞으로는 스택의 개념만 쓰도록 하자
그럼 짱빠르다 굿
소스코드(스택 진짜 씀;;;;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <iostream> #include <algorithm> #include <stack> #include <string> using namespace std; int main() { stack<char> st; string s, t; cin >> s >> t; int n = s.size(); int m = t.size(); reverse(t.begin(), t.end()); for (int i = 0; i < n; i++) { st.push(s[i]); if (s[i] == t[0]) { string temp = ""; int c = t.size(); while (!st.empty() && c--) { temp += st.top(); st.pop(); } if (t == temp) continue; reverse(temp.begin(), temp.end()); for (int j = 0; j < temp.size(); j++) st.push(temp[j]); } } string ans = ""; while (!st.empty()) { ans += st.top(); st.pop(); } reverse(ans.begin(), ans.end()); cout << ans; } | cs |
소스코드(스택 개념만 뽑아씀)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> #include <string.h> using namespace std; char S[1000010], T[111], st[1000010]; int main() { scanf(" %s %s", &S, &T); int n = strlen(S); int m = strlen(T); int pos = 0; for (int i = 0; i < n; i++) { st[pos++] = S[i]; st[pos] = 0; if (pos >= m && strcmp(st + (pos - m), T) == 0) { pos -= m; st[pos] = 0; } } printf("%s\n", st); } | cs |
'알고리즘 > BOJ' 카테고리의 다른 글
[백준] 10677 It's All About the Base (0) | 2018.06.29 |
---|---|
[백준] 10748 Cow Hopscotch (0) | 2018.06.29 |
[백준] 13264 접미사 배열2 (4) | 2018.06.27 |
[백준] 13904 과제 (0) | 2018.06.27 |
[백준] 15594 Out of place (0) | 2018.06.26 |
댓글