답안 #148225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
148225 2019-08-31T18:04:02 Z claudy Necklace (Subtask 1-3) (BOI19_necklace1) C++14
25 / 85
569 ms 164948 KB
# pragma GCC optimize("Ofast,no-stack-protector")
# pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
# pragma GCC optimize("Ofast")
# pragma GCC optimization ("unroll-loops")
# include "bits/stdc++.h"
/*
# include <ext/pb_ds/tree_policy.hpp>
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/rope>
*/
std::pair<int,int> DR[] = {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define rcg(s) cout << s;exit(0)
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define db(x) cerr << #x << " = " << x << '\n'
# define pb push_back
# define mp make_pair
# define all(s) s.begin(),s.end()
# define sz(x) (int)((x).size())
# define int ll
using namespace std;
 
/*
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
Tree<int>tr;
*/
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# define LOCAL
# define sim template < class c
# define ris return * this
# define dor > debug & operator <<
# define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define show(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
int gcd(int a, int b)
{
if(b) return gcd(b,a%b);
return a;
}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
// 5:15 - necklace
 
int p[405],inv[405];
const int P = 31;
const int mod = 20000003;
string a,b;
 
bitset<20000003>f[405]; 

int my_pow(int x,int p){
	int ans = 1;
	for(int i = 0;(1 << i) <= p;i++){
		if(p & (1 << i)){
			ans *= x;
			ans %= mod;
		}
		x *= x;
		x %= mod;
	}
	return ans;
}
 
pair<int,int> check(int l,int r){
	string s = "";
	if(r >= sz(a)) return mp(0,0);
	for(int i = l;i <= r;i++){
		s += a[i];
	}
	int x = 0;
	for(int i = 0;i < sz(s);i++){
		x += (s[i] - 'a') * p[i];
		x %= mod; 
	}
	for(int i = 0;i < sz(s);i++){
		int y = f[r - l + 1][x];
		//if(y) cout << i << ' ' << x << '\n';
		if(y) return mp(y,x);
		x -= (s[i] - 'a');
		x %= mod;
		if(x < 0) x += mod;
		x *= inv[1];
		x %= mod;
		x += (s[i] - 'a') * p[sz(s) - 1];
		x %= mod;
	}
	return mp(0,0);
}

int hashish(string s){
	int x = 0;
	for(int i = 0;i < sz(s);i++){
		x += (s[i] - 'a') * p[i];
		x %= mod; 
	}
	return x;
}

 
int32_t main(){_
	//freopen("input","r",stdin);
	cin >> a >> b;
	int n = max(sz(a),sz(b));
	p[0] = 1;
	for(int i = 1;i <= n;i++){
		p[i] = (p[i - 1] * P) % mod;
		inv[i] = my_pow(p[i],mod - 2);
	}
	for(int i = 0;i < sz(b);i++){
		int x = 0;
		for(int j = i;j < sz(b);j++){
			x += (b[j] - 'a') * p[j - i];
			x %= mod;
			f[j - i + 1][x] = 1;
		}
	}
	reverse(b.begin(),b.end());
	for(int i = 0;i < sz(b);i++){
		int x = 0;
		for(int j = i;j < sz(b);j++){
			x += (b[j] - 'a') * p[j - i];
			x %= mod;
			f[j - i + 1][x] = 1;
		}
	}
	//cout << hashish("cdab") << '\n';
	//rc(check(0,3).first);
	
	pair<int,pair<int,int>>mxlen = mp(0,mp(0,0));
	for(int i = 0;i < sz(a);i++){
		for(int j = 1;j <= sz(a);j++){
			pair<int,int> x = check(i,i + j - 1);
			if(x.first){
				mxlen = max(mxlen,mp(j,mp(i,x.second)));
			}
		}
	}
	cout << mxlen.first << '\n' << mxlen.second.first << ' ';
	reverse(b.begin(),b.end());
	for(int i = 0;i < sz(b);i++){
		int x = 0;
		for(int j = i;j < sz(b);j++){
			x += (b[j] - 'a') * p[j - i];
			x %= mod;
			if(x == mxlen.second.second) rc(i);
		}
	}
	reverse(b.begin(),b.end());
	for(int i = 0;i < sz(b);i++){
		int x = 0;
		for(int j = i;j < sz(b);j++){
			x += (b[j] - 'a') * p[j - i];
			x %= mod;
			if(x == mxlen.second.second) rc(sz(b) - j - 1);
		}
	}
	return 0;
}
 
 
 
 
 
 
 
 
 

Compilation message

necklace.cpp:4:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
 # pragma GCC optimization ("unroll-loops")
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 7928 KB Output is correct
2 Correct 54 ms 9464 KB Output is correct
3 Correct 41 ms 28792 KB Output is correct
4 Correct 62 ms 20728 KB Output is correct
5 Correct 87 ms 33144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 7928 KB Output is correct
2 Correct 54 ms 9464 KB Output is correct
3 Correct 41 ms 28792 KB Output is correct
4 Correct 62 ms 20728 KB Output is correct
5 Correct 87 ms 33144 KB Output is correct
6 Incorrect 569 ms 164948 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 7928 KB Output is correct
2 Correct 54 ms 9464 KB Output is correct
3 Correct 41 ms 28792 KB Output is correct
4 Correct 62 ms 20728 KB Output is correct
5 Correct 87 ms 33144 KB Output is correct
6 Incorrect 569 ms 164948 KB Output isn't correct
7 Halted 0 ms 0 KB -