Submission #967540

# Submission time Handle Problem Language Result Execution time Memory
967540 2024-04-22T11:29:17 Z 8pete8 Necklace (Subtask 1-3) (BOI19_necklace1) C++17
9 / 85
1500 ms 163924 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
#include <cstdlib> 
#include <cstdint>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
#define double long double
const int mod=1e9+7,mxn=4e3+5,lg=60,inf=1e18,minf=-1e18;
int base=101;
int p[mxn+10],invp[mxn+10],prefa[mxn+10],prefb[mxn+10];
string a,b;
int geta(int l,int r){
	int x=prefa[r];
	if(l){
		x=(x-prefa[l-1]+mod)%mod;
		x=(x*invp[l])%mod;
	}
	return x;
}
int getb(int l,int r){
	int x=prefb[r];
	if(l){
		x=(x-prefb[l-1]+mod)%mod;
		x=(x*invp[l])%mod;
	}
	return x;
}
int inv(int x){
	int ex=mod-2,ans=1;
	while(ex){
		if(ex&1)ans=(ans*x)%mod;
		x=(x*x)%mod;
		ex>>=1;
	}
	return ans;
}
vector<int>posb[27],posa[27];
int mx[mxn+10];
vector<pii>have[mxn+10];
int get(int p1,int p2){
	int l=0,r=min(a.size()-p1-1,b.size()-p2-1),ans=minf;
	while(l<=r){
		int mid=l+(r-l)/2;
		if(geta(p1,p1+mid)==getb(p2,p2+mid))l=mid+1,ans=max(ans,mid);
		else r=mid-1;
	}
	return ans;
}
struct seg{
	int v[2*mxn+10];
	//keep (r-l) 
	void init(int n){for(int i=0;i<=2*n;i++)v[i]=inf;}
	void update(int pos,int val){
		pos+=a.size();
		v[pos]=min(v[pos],val);
		for(int i=pos;i>0;i>>=1)v[i>>1]=max(v[i],v[i^1]);
	}
	int qry(int l,int r){
		int ans=inf;
		for(l+=a.size(),r+=a.size();l<=r;l>>=1,r>>=1){
			if(l&1)ans=min(ans,v[l++]);
			if(!(r&1))ans=min(ans,v[r--]);
		}
		return ans;
	}
}t[mxn+10];//b.size()
pair<int,pii>ans={0,{0,0}};
bool yes=0,yes2=0;
void solve(){
	for(int i=0;i<27;i++)posa[i].clear(),posb[i].clear();
	for(int i=0;i<a.size();i++){
		prefa[i]=(p[i]*a[i])%mod;
		if(i)prefa[i]=(prefa[i]+prefa[i-1])%mod;
	}
	for(int i=0;i<b.size();i++){
		prefb[i]=(p[i]*b[i])%mod;
		if(i)prefb[i]=(prefb[i]+prefb[i-1])%mod;
	}
	for(int i=0;i<a.size();i++)posa[a[i]-'a'].pb(i);
	for(int i=0;i<b.size();i++)posb[b[i]-'a'].pb(i);
	for(int i=0;i<b.size();i++)t[i].init(a.size());
	for(int i=0;i<a.size();i++){
		for(auto j:posb[a[i]-'a']){
			int x=get(i,j);
			t[j].update(i+x,i);//update 
			int k=j;
			if(yes)k=(b.size()-1)-(j+x);
			if(yes2)ans=max(ans,{x+1,{k,i}});
			else ans=max(ans,{x+1,{i,k}});
			if(j+x+1>=b.size())continue;
			int y=t[j+x+1].qry(max(0LL,i-1),a.size());
			if(y==inf)continue;
			int l=i-y;
			//cout<<i<<" "<<j<<' '<<y<<" "<<x<<' '<<l<<'\n';
			if(yes)j=(b.size()-1)-(j+x+l);
			if(yes2)ans=max(ans,{x+l+1,{j,y}});
			else ans=max(ans,{x+l+1,{y,j}});
		}	
	}
	/*
	find maxmatch
	for(i ina)for(j inb){
		pos2 ina where mxmatch(pos2,j+1mxmatch)>=curpos
		and mxmatch is max
	}
	when we know j+1+mxmatch
	we can have list of the matched matching with j{
		range l,r;
		want to find max(r-l) where l<=pos<=r
		if we add inorder then all l<=pos
		then we can only keep r
		we can keep segtree and qry max on (pos,r)
	}
	*/
}
int32_t main(){
	fastio
	cin>>a>>b;
	int d=inv(base);
	p[0]=invp[0]=1;
	for(int i=1;i<max(a.size(),b.size());i++)p[i]=(p[i-1]*base)%mod,invp[i]=(invp[i-1]*d)%mod;
	solve();
	reverse(all(b));
	yes=1;
	solve();
	reverse(all(b));
	swap(a,b);
	yes2=1;
	yes=0;
	solve();
	reverse(all(b));
	yes=1;
	solve();
	reverse(all(b));
	swap(a,b);
	cout<<ans.f<<" "<<ans.s.f<<" "<<ans.s.s<<'\n';
}
/*

*/

Compilation message

necklace.cpp: In function 'void solve()':
necklace.cpp:101:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |  for(int i=0;i<a.size();i++){
      |              ~^~~~~~~~~
necklace.cpp:105:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |  for(int i=0;i<b.size();i++){
      |              ~^~~~~~~~~
necklace.cpp:109:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |  for(int i=0;i<a.size();i++)posa[a[i]-'a'].pb(i);
      |              ~^~~~~~~~~
necklace.cpp:110:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |  for(int i=0;i<b.size();i++)posb[b[i]-'a'].pb(i);
      |              ~^~~~~~~~~
necklace.cpp:111:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |  for(int i=0;i<b.size();i++)t[i].init(a.size());
      |              ~^~~~~~~~~
necklace.cpp:112:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |  for(int i=0;i<a.size();i++){
      |              ~^~~~~~~~~
necklace.cpp:120:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |    if(j+x+1>=b.size())continue;
      |       ~~~~~^~~~~~~~~~
necklace.cpp: In function 'int32_t main()':
necklace.cpp:151:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'const long unsigned int' [-Wsign-compare]
  151 |  for(int i=1;i<max(a.size(),b.size());i++)p[i]=(p[i-1]*base)%mod,invp[i]=(invp[i-1]*d)%mod;
      |              ~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 7 ms 6744 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Partially correct 1 ms 6720 KB Output is partially correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 7 ms 6744 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Partially correct 1 ms 6720 KB Output is partially correct
6 Correct 49 ms 27228 KB Output is correct
7 Partially correct 28 ms 27484 KB Output is partially correct
8 Partially correct 41 ms 25240 KB Output is partially correct
9 Correct 26 ms 27224 KB Output is correct
10 Correct 7 ms 27228 KB Output is correct
11 Correct 9 ms 25176 KB Output is correct
12 Correct 29 ms 25268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 7 ms 6744 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Partially correct 1 ms 6720 KB Output is partially correct
6 Correct 49 ms 27228 KB Output is correct
7 Partially correct 28 ms 27484 KB Output is partially correct
8 Partially correct 41 ms 25240 KB Output is partially correct
9 Correct 26 ms 27224 KB Output is correct
10 Correct 7 ms 27228 KB Output is correct
11 Correct 9 ms 25176 KB Output is correct
12 Correct 29 ms 25268 KB Output is correct
13 Execution timed out 1564 ms 163924 KB Time limit exceeded
14 Halted 0 ms 0 KB -