티스토리 뷰
16172번: 나는 친구가 적다 (Large)
첫 번째 줄에는 알파벳 소문자, 대문자, 숫자로 이루어진 문자열 S가 주어진다. (1 ≤ |S| ≤ 200,000) 두 번째 줄에는 성민이가 찾고자 하는 알파벳 소문자, 대문자로만 이루어진 키워드 문자열 K가
www.acmicpc.net
백준 소스코드 [C++] 16172 나는 친구가 적다 (Large)
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
vector<int> getPi(string sub) {
vector<int> pi(sub.size(), 0);
for (int sur = 1, pre = 0; sur < sub.size(); sur++) {
while (pre > 0 && sub[sur] != sub[pre]) pre = pi[--pre];
if (sub[sur] == sub[pre]) pi[sur] = ++pre;
}
return pi;
}
vector<int> kmp(string target, string sub) {
vector<int> res;
vector<int> pi = getPi(sub);
for (int i = 0, j = 0; i < target.size(); i++) {
while (j > 0 && target[i] != sub[j]) j = pi[--j];
if (target[i] == sub[j]) {
if (j + 1 == sub.size()) {
res.push_back(i - j);
j = pi[j];
}
else j++;
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
string target, sub,real="";
getline(cin, target);
getline(cin, sub);
for (int i = 0;i < target.size();i++) if (!('0' <= target[i] && target[i] <= '9')) real += target[i];
vector<int> res = kmp(real, sub);
if (res.size()) cout << "1";
else cout << "0";
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 2581 소수 (0) | 2020.11.09 |
---|---|
백준 소스코드 [C++] 1305 광고 (0) | 2020.11.09 |
백준 소스코드 [C++] 16134 조합 (Combination) (0) | 2020.11.08 |
백준 소스코드 [C++] 15791 세진이의 미팅 (0) | 2020.11.08 |
백준 소스코드 [C++] 13977 이항 계수와 쿼리 (0) | 2020.11.08 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- WWDC19
- 다익스트라 시간복잡도
- mach-o
- 벨만포드 알고리즘
- 에드몬드 카프 알고리즘
- MeTal
- WWDC21
- 코딩대회
- 최단경로 문제
- 포드 풀커슨 알고리즘
- 강한 순환 참조
- CPU와 Memory
- IOS
- 최단경로 알고리즘
- State Restoration
- Testable
- 부스트캠프 6기
- 컴퓨터 추상화
- HIG
- test coverage
- 최단경로문제
- observeOn
- rxswift
- WWDC17
- 네트워크 플로우
- 최대 매칭
- CompositionalLayout
- WWDC16
- 네트워크 유량
- 벨만포드 시간복잡도
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함