跳到正文

题解

题解 CF1286C2 【Madhouse (Hard version)】

Purple_wzy约 5 分钟
原文
文章信息与版本

文章信息

发布
2020年1月7日
收录
2026年3月16日
更新
2026年11月17日
篇幅
约 5 分钟
题目
CF1286C2

版本

  1. v2当前
  2. v1

C Madhouse

题解:首先考虑简单版。

我们可以询问[1,n][1,n]以及[1,n−1][1,n-1]。

发现第一次询问比第二次多了nn个串,这nn个串恰好是 这个串的nn个后缀的乱序版本。

找到这nn个串即可还原整个串。

总串数:O(n2n^2)

对于困难版,我们可以采用同样的方法,先求出[1,n/2][1,n/2]这部分的串。 然后再询问[1,n][1,n]这部分。

设f[i][x]f[i][x]表示这次询问所有长度为i的串中,字符xx的出现次数。

考虑f[i+1][x]−f[i][x]f[i+1][x]-f[i][x]的意义,发现这个东西即为原串[i+1,n−i][i+1,n-i]这段区间内字符xx的出现次数。

因为我们已经求出了前半段,后半段可以通过倒序枚举ii逐个求出。

总串数:O(0.75*n2n^2)

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define re register int
#define F(x,y,z) for(re x=y;x<=z;x++)
#define FOR(x,y,z) for(re x=y;x>=z;x--)
typedef long long ll;
#define I inline void
#define IN inline int
#define C(x,y) memset(x,y,sizeof(x))
#define STS system("pause")
template<class D>I read(D &res){
	res=0;register D g=1;register char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')g=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		res=(res<<3)+(res<<1)+(ch^48);
		ch=getchar();
	}
	res*=g;
}
map<string,int>mp;
struct P{
	string a;
	friend bool operator > (P x,P y){
		return x.a.size()==y.a.size()?x.a.compare(y.a)>0:x.a.size()>y.a.size();
	}
}p;
priority_queue<P,vector<P>,greater<P> >q;
char c[110],ans[110];
string s;
int n,m,cnt[30],now[30],f[110][30];
namespace solve1{
	I solve(int x){
		if(x==1){
			cout<<"? 1 1"<<endl;fflush(stdout);
			cin>>c+1;ans[1]=c[1];return;
		}
		cout<<"? 1 "<<x<<endl;fflush(stdout);
		F(i,1,x*(x+1)/2){
			cin>>c+1;m=strlen(c+1);sort(c+1,c+1+m);p.a.clear();p.a.append(c+1);q.emplace(p);
		}
		cout<<"? 1 "<<x-1<<endl;fflush(stdout);
		F(i,1,x*(x-1)/2){
			cin>>c+1;m=strlen(c+1);sort(c+1,c+1+m);s.clear();s.append(c+1);mp[s]++;
		}
		C(cnt,0);m=0;
		while(!q.empty()){
			s=q.top().a;q.pop();//cout<<"@"<<s<<endl;
			if(mp.count(s)){
				if(mp[s]==1)mp.erase(s);
				else mp[s]--;
				continue;
			}
			C(now,0);
			F(i,0,s.size()-1){
				now[s[i]-'a'+1]++;
			}
			F(i,1,26)if(now[i]>cnt[i]){ans[++m]=i+'a'-1;cnt[i]++;break;}
		}
		reverse(ans+1,ans+1+x);
	}
};
namespace solve2{
	I solve(){
		cout<<"? 1 "<<n<<endl;fflush(stdout);
		F(i,1,n*(n+1)>>1){
			cin>>c+1;m=strlen(c+1);
			F(j,1,m)f[m][c[j]-'a'+1]++;
		}
		re l,r;
		FOR(i,(n-1)>>1,0){
			l=i+1;r=n-i;
			C(cnt,0);
			F(j,l,r-1)cnt[ans[j]-'a'+1]++;
			F(j,1,26)if(f[i+1][j]-f[i][j]>cnt[j]){ans[r]=j+'a'-1;break;}
		}
		cout<<"! "<<ans+1<<endl;fflush(stdout);
	}
};
int main(){
	read(n);
	if(n==1){
		cout<<"? 1 1"<<endl;fflush(stdout);
		cin>>c+1;cout<<"! "<<c[1]<<endl;fflush(stdout);
		return 0;
	}
	solve1::solve(n/2);
	solve2::solve();
	return 0;
}

更好的阅读体验:https://www.cnblogs.com/Purple-wzy/

原文