跳到正文

题解

QOJ17714. Cat Cut 题解

zrl123456约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年9月16日
收录
2026年9月16日
更新
2026年11月21日
篇幅
约 7 分钟

版本

  1. v1当前
Hint 1

考虑刻画最终答案在原来字符串上对应的位置。

Hint 2

我们考虑通过 bitset 配合二分答案解决这个问题。

对应 Hint 1:

我们考虑刻画最终答案在原来字符串上对应的位置。

我们首先考虑到,每个字符串贡献的一定是一个子串,这是显然的。

进一步观察,我们发现,除了第一个贡献非空的字符串,其它字符串贡献的都是前缀。

这个难道就是正确的形式吗?我们观察样例是错误的,进一步思考,我们发现,如果第一个贡献非空的字符串就是第一个字符串,那么这个贡献还必须是后缀。

这样我们就成功刻画了答案的形式。

对应 Hint 2:

我们考虑预处理出某个位置能否作为这个字符串的某个位置,配合 bitset 可以做到 Θ(nm2w)\Theta(\frac{nm^2}{w})。

然后接下来,我们考虑对于每一个位置,二分这个位置的答案,然后我们配合上面预处理出来的 bitset 可以解决这个问题。

这样我们就做完了这道题。
时间复杂度为 Θ(nm2log⁡∑w)\Theta(\frac{nm^2\log\sum}{w}),空间复杂度为 Θ(nm2w)\Theta(\frac{nm^2}{w})。

code
C++
#include<bits/stdc++.h>
#define inl inline
#define int long long 
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i) 
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i) 
#define endl '\n'
#define INF 1e18
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front 
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int> 
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream 
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=2005,M=30;
string s[N];
bitset<N>ch[N][M],all,vis[N][N],now[2][N];
int n;
inl bool check(int k,int cur){
	rep(i,1,n)
		if((now[cur][i]&ch[i][k]).any()) return 1;
	return 0;
}
inl int lower(int l,int r,int cur){
	int ans=-1;
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid,cur)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	return ans;
}
si main(){
	fst;
	int m;
	string ans="";
	cin>>n>>m;
	rep(i,1,n) cin>>s[i];
	rep(i,1,n) s[i]=' '+s[i];
	rep(i,1,n)
		rep(j,1,m) ch[i][s[i][j]-'a'][j]=1;
	rep(i,1,25)
		rep(j,1,n) ch[j][i]|=ch[j][i-1];
	rep(i,1,m) all[i]=1;
	rep(i,1,n) vis[m][i]=all;
	per(i,m-1,0){
		bool flagj=0;
		per(j,n,1){
			vis[i][j]=(vis[i+1][j]>>1)&all;
			if(flagj)
				if(j!=1) vis[i][j]=all;
				else vis[i][j][m]=1;
			if(vis[i+1][j][1]) flagj=1;
		}
	} 
	rep(i,1,n) now[0][i]=vis[0][i];
	rep(i,1,m){
		int c=lower(0,25,i-1&1);
		ans+=char('a'+c);
		rep(j,1,n) now[i-1&1][j]&=ch[j][c];
		rep(x,1,n) now[i&1][x]=0;
		bool flagj=0;
		rep(j,1,n){
            now[i&1][j]|=(now[i-1&1][j]<<1)&all;
            if (flagj) now[i&1][j][1]=1;
            if(j==1&&now[i-1&1][1][m]||j>1&&now[i-1&1][j].any()) flagj=1;
        } 
        rep(j,1,n) now[i&1][j]&=vis[i][j]; 
//        rep(x,1,n){
//        	rep(y,1,m) cout<<now[i&1][x][y];
//        	cout<<endl;
//		} 
//		cout<<endl;
	}
	cout<<ans<<endl;
	return 0;
}
原文