(交互题)CF1282D. Enchanted Artifact

CF1282D. Enchanted Artifact

题意&思路:

一个只包含a,b的字符串长度n不超过300,删除,添加,改变字符都算一次操作。现在有一个字符串s,你去猜。你每次输给测评姬一个字符串,测评姬返回给你需要多少次操作可以得到s。你有n+2次机会(你不知道n是多少)。
第一次做交互题,格式都不知道怎么写,QAQ。
一开始看到2300难度的题目,我是拒绝的。
一开始想由于不知道字符串长度,可以先输入”a“得到n,”b“得到m。如果n≠m,那么字符串肯定是min(n,m)个a或者b。如果n=m,则说明字符串长度为n+1。然后我们输出n+1个a就可以得到一个k,然后知道这个字符串有k个b,然后按位枚举b。但是一算最多需要n+1+3次,超过了题目的要求。
然后转念一想,没必要第二次输出”b",直接输出n+1个a,如果的到k=n+1,那字符串肯定是n个b。其他的k就表明字符串有几个b,然后从头到尾枚举,如果得到的次数增多了,就表明这个位置是a,否则是b。
然后最最最坑的一个点就是,n≤300,所以如果一个长为300的字符串全是b,按照程序,他第一次会输出300,然后第二次输出301,超过300,直接报WA。那么加一个特判就行了。

代码:

#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define cl(x,y) memset(x,y,sizeof(x))
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
const int N=1e6+10;
const int mod=1e7+9;
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	string s;
	int n,m,i;
	cout<<"a"<<endl;
	fflush(stdout);
	cin>>n;
	if(n==0)
		exit(0);
	if(n==300)
	{
		for(i=1;i<=300;i++)
			cout<<"b";
		cout<<endl;
		cin>>m;
		exit(0);
	}
	for(i=0;i<=n;i++)
		s+='a';
	cout<<s<<endl;
	fflush(stdout);
	cin>>m;
	if(m==0)
		exit(0);
	if(m>n)
	{
		for(i=1;i<=n;i++)
			cout<<"b";
		cout<<endl;
		fflush(stdout);
		cin>>m;
		exit(0);
	}
	int pre=m,temp;
	for(i=0;i<=n;i++)
	{
		s[i]='b';
		cout<<s<<endl;
		fflush(stdout);
		cin>>temp;
		if(temp==0)
			exit(0);
		if(temp>=pre)
			s[i]='a';
		else
			pre=temp;
	}
	return 0;
}

发布了119 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Z7784562/article/details/104162290