답안 #524684

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524684 2022-02-09T19:23:29 Z fcw Necklace (Subtask 4) (BOI19_necklace4) C++17
15 / 15
373 ms 552 KB
#include <bits/stdc++.h>
#define st first
#define nd second
using lint = int64_t;
constexpr int MOD = int(1e9) + 7;
constexpr int INF = 0x3f3f3f3f;
constexpr int NINF = 0xcfcfcfcf;
constexpr lint LINF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
	return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;

template<typename T> struct kmp_t {
  vector<T> word; vector<int> failure;
  template<typename I> kmp_t(I begin, I end) {
	for (I iter = begin; iter != end; ++iter) word.push_back(*iter);
    int n = int(size(word)); failure.resize(n+1, 0);
    for (int s = 2; s <= n; ++s) {
        failure[s] = failure[s-1];
        while (failure[s] > 0 && word[failure[s]] != word[s-1])
            failure[s] = failure[failure[s]];
        if (word[failure[s]] == word[s-1]) failure[s] += 1;
    }
  }
};

array<int, 3>solve(string s, string t){
	array<int, 3>ans = {0, -1, -1};
	int n = (int)s.size(), m = (int)t.size();
	string rt = t;
	reverse(rt.begin(), rt.end());
	for(int i=0;i<=n;i++){
		string l = s.substr(0, i), r = s.substr(i, n-i);
		int nl = i, nr = n-i;
		reverse(l.begin(), l.end());
		l += "#" + rt, r += "#" + t;
		vector<int>fl = kmp_t<int>(l.begin(), l.end()).failure;
		vector<int>fr = kmp_t<int>(r.begin(), r.end()).failure;
		for(int j=0;j<=m;j++){
			int k = m-j;
			if(fr[nr+j+1] + fl[nl+k+1] > ans[0]){
				ans = {fr[nr+j+1] + fl[nl+k+1], i-fl[nl+k+1], j-fr[nr+j+1]};
			}
		}
	}
	return ans;
}

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	string s, t;
	cin>>s>>t;
	int n = (int)s.size(), m = (int)t.size();
	array<int, 3>ans = solve(s, t);
	reverse(t.begin(), t.end());
	array<int, 3>rans = solve(s, t);
	if(ans[0] >= rans[0]){
		cout<<ans[0]<<"\n"<<ans[1]<<" "<<ans[2]<<"\n";
	}
	else{
		cout<<rans[0]<<"\n"<<rans[1]<<" "<<m-(rans[2]+rans[0])<<"\n";
	}

	return 0;
}
/*
[  ]Leu o problema certo???
[  ]Ver se precisa de long long
[  ]Viu o limite dos fors (é n? é m?)
[  ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[  ]Testar sample
[  ]Testar casos de  borda
[  ]1LL no 1LL << i
[  ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/

Compilation message

necklace.cpp: In function 'int main()':
necklace.cpp:56:6: warning: unused variable 'n' [-Wunused-variable]
   56 |  int n = (int)s.size(), m = (int)t.size();
      |      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 352 ms 436 KB Output is correct
2 Correct 313 ms 432 KB Output is correct
3 Correct 373 ms 440 KB Output is correct
4 Correct 301 ms 544 KB Output is correct
5 Correct 244 ms 552 KB Output is correct
6 Correct 264 ms 404 KB Output is correct
7 Correct 294 ms 444 KB Output is correct
8 Correct 312 ms 420 KB Output is correct
9 Correct 323 ms 412 KB Output is correct