이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;
const int MAX = 10010;
int n, len;
int arr[MAX];
int q, sim;
int res[MAX];
vi KMP(vi str){
int l = szof(str);
vi ret(l);
ret[0] = -1;
int pos = -1;
FOR(i, 1, l-1, 1){
while(pos != -1 and str[pos+1] != str[i]){
pos = ret[pos];
}
if(str[pos+1] == str[i]){
pos++;
}
ret[i] = pos;
}
return ret;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>len;
FOR(i, 0, n-1, 1){
cin>>arr[i];
}
cin>>q;
while(q--){
cin>>sim;
FOR(i, 0, n-1, 1){
vi sstr(arr+i, arr+n);
vi kmp = KMP(sstr);
int dp[MAX];
FOR(i, 0, len-2, 1){
dp[i] = -1;
}
dp[len-1] = 1;
FOR(i, len, szof(kmp)-1, 1){
if(kmp[i]!=-1 and dp[kmp[i]]==1){
dp[i] = 1;
}
else{
dp[i] = -1;
}
}
FOR(j, i+1, n-len, 1){
int K = kmp[j - i + len - 1];
if(K != -1 and dp[K] == 1){
res[i]++;
res[j]++;
//cerr<<"("<<i<<", "<<j<<") "<<endl;
}
}
/*
cerr<<"kmp("<<i<<") : ";
for(int i : kmp){
cerr<<i<<" ";
}
cerr<<endl;
*/
}
FOR(i, 0, n-len, 1){
cout<<res[i]<<" ";
}
cout<<'\n';
}
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... |