Submission #437546

# Submission time Handle Problem Language Result Execution time Memory
437546 2021-06-26T13:53:27 Z codebuster_10 Necklace (Subtask 4) (BOI19_necklace4) C++17
15 / 15
331 ms 504 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int int64_t //be careful about this 
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)
 
#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define eb emplace_back
#define LB lower_bound  
#define UB upper_bound
#define PQ priority_queue
 
#define SZ(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem(a,b) memset(a, b, sizeof(a))
 
template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}
 
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
 
void setIO(string s = "") {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
  // remove this when size of input is not fixed.
  cin.exceptions(cin.failbit); 
	cout.precision(15);	cout << fixed;
  if(SZ(s)){
  	freopen((s+".in").c_str(),"r",stdin);
  	freopen((s+".out").c_str(),"w",stdout);
  }
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
namespace kmp {
  // Returns the next length to search from after starting from `len` and adding char `c`.
  // Runs in worst case O(len) but amortizes to O(1) in most situations.
  template<typename T, typename T_elem>
  int get_link(const T &pattern, const vector<int> &fail, int len, const T_elem &c) {
  	while(len > 0 && pattern[len] != c)	len = fail[len];
 		if(pattern[len] == c)	len++;
 		return len;
  }
 
  // Computes the failure function on `pattern` so that we can search for it in future strings.
  template<typename T>
  vector<int> compute_failure_function(const T &pattern) {
  	// fail[i] = for the prefix [0, i) of `pattern`, the length of the longest proper prefix that is also a suffix.
    int n = int(pattern.size());
    vector<int> fail(n + 1, 0);
    int len = 0;
 
    for(int i = 1; i < n; i++) {
    	len = get_link(pattern, fail, len, pattern[i]);
      fail[i + 1] = len;
    }
 
    return fail;
  }
}
 
 
 
 
 
 
 
const char DELIMITER = '#';
 
 
void solve(const string s,const string t, int& max_length, int& start_s, int& start_t, bool reversed_t){
	
	int n = SZ(s), m = SZ(t);
	
	string sa, sb;
	vt<int> va, vb;
	
	for(int i = 0; i <= n; ++i){
		sa.clear(); sb.clear();
		va.clear(); vb.clear();
		
		for(int j = i; j < n; ++j) sa.pb(s[j]);
		sa.pb(DELIMITER);
		sa += t;
		
		sb = t;
		sb.pb(DELIMITER);
		for(int j = 0; j < i; ++j) sb.pb(s[j]);
		
		reverse(all(sb));
		
		va = kmp::compute_failure_function(sa);
		vb = kmp::compute_failure_function(sb);
		
		int x = i, y = n - i;
		for(int j = 0; j <= m; ++j){
			int tmp = va[y+1+j] + vb[x+1+m-j];
			if(ckmax(max_length,tmp)){
				start_s = i - vb[x+1+m-j];
				start_t = j - va[y+1+j];
				if(reversed_t){
					int end_t = j + vb[x+1+m-j] - 1;
					start_t = m - 1 - end_t;
				}
			}
		}
	}
	return;
}
 
 
signed main(){
  setIO();
  string s,t;
  rd(s,t);
  
  int max_length = 0, start_s = -1, start_t = -1;
 
	solve(s,t,max_length,start_s,start_t,false);
	reverse(all(t));
  solve(s,t,max_length,start_s,start_t,true);
  
  cout << max_length << endl;
  cout << start_s << ' ' << start_t; 
}

Compilation message

necklace.cpp: In function 'void setIO(std::string)':
necklace.cpp:39:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |    freopen((s+".in").c_str(),"r",stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
necklace.cpp:40:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |    freopen((s+".out").c_str(),"w",stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 325 ms 460 KB Output is correct
2 Correct 297 ms 464 KB Output is correct
3 Correct 331 ms 476 KB Output is correct
4 Correct 252 ms 460 KB Output is correct
5 Correct 269 ms 468 KB Output is correct
6 Correct 290 ms 460 KB Output is correct
7 Correct 272 ms 496 KB Output is correct
8 Correct 288 ms 492 KB Output is correct
9 Correct 291 ms 504 KB Output is correct