이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N,K;
vector<int> getPartialMatch(const vector<int>& N) {
int m = N.size();
vector<int> pi(m, 0);
int begin = 1, matched = 0;
while (begin + matched < m) {
if (N[begin+matched] == N[matched]) {
++matched;
pi[begin+matched-1] = matched;
} else {
if (matched == 0)
++begin;
else {
begin += matched - pi[matched-1];
matched = pi[matched-1];
}
}
}
return pi;
}
bool kmpSearch(const vector<int>& H, const vector<int>& N) {
int n = H.size(), m = N.size();
vector<int> pi = getPartialMatch(N);
for (int jump=0; jump<=n-K; jump+=K) {
int begin = 0, matched = 0, begin2 = 0;
while (begin <= n - K) {
if (matched < m && H[begin + matched] == N[matched+jump]) {
++matched;
if (matched >= K) return true;
} else {
if (matched == 0)
++begin;
else {
begin += matched - pi[matched-1];
matched = pi[matched-1];
}
}
}
}
return false;
}
vector<int> codes[1001];
vector<int> codesR[1001];
int main() {
cin >> N >> K;
for (int i=0; i<N; ++i) {
int n; cin >> n;
for (int j=0; j<n; ++j) {
int num; cin >> num;
codes[i].push_back(num);
codesR[i].push_back(num);
}
reverse(codesR[i].begin(), codesR[i].end());
}
int s = 0;
for (int i=0; i<N; ++i) {
for (int j=0; j<N; ++j) {
if (i == j) continue;
if (kmpSearch(codes[i], codes[j]) || kmpSearch(codes[i], codesR[j])) {
s++;
}
if (s == K) {
cout << "YES"; return 0;
}
}
}
cout << "NO";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |