字符串 分类讨论 01字符串 1890 B. Qingshan Loves Strings

#include

using namespace std;

void solve()
{
	int n,m;
	cin>>n>>m;
	
	string s,t;
	cin>>s>>t;
	
	if(n==1)
	{
		cout<<"YES"<<endl;
		return;
	}
	
	bool flag=false;
	for(int i=0;i<m-1;i++)
		if(t[i]==t[i+1])
			flag=true;
	
	if(flag)
	{
		cout<<"NO"<<endl;
		return;
	}
	
	int cnt_0=0,cnt_1=0;
	for(int i=0;i<n-1;i++)
		if(s[i]==s[i+1])
		{
			if(s[i]=='0')	
				cnt_0++;
			else
				cnt_1++;
		}
	
	if(cnt_0>=1&&cnt_1==0)
	{
		if(t[0]=='1'&&t[m-1]=='1')
		{
			cout<<"YES"<<endl;
			return;
		}
		else
		{
			cout<<"NO"<<endl;
			return;
		}
	}
	else if(cnt_0==0&&cnt_1>=1)
	{
		if(t[0]=='0'&&t[m-1]=='0')
		{
			cout<<"YES"<<endl;
			return;
		}
		else
		{
			cout<<"NO"<<endl;
			return;
		}
	}
	else
	{
		cout<<"NO"<<endl;
		return;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	cin>>t;
	
	while(t--)
		solve();
	
	return 0;
}

奇怪,思路清晰,但是在第二个测试点错了

#include

using namespace std;

void solve()
{
	int n,m;
	cin>>n>>m;
	
	string s,t;
	cin>>s>>t;
	
	if(n==1)
	{
		cout<<"YES"<<endl;
		return;
	}
	
	bool flag=false;
	for(int i=0;i<m-1;i++)
		if(t[i]==t[i+1])
			flag=true;
	
	if(flag)
	{
		cout<<"NO"<<endl;
		return;
	}
	
	int cnt_0=0,cnt_1=0;
	for(int i=0;i<n-1;i++)
		if(s[i]==s[i+1])
		{
			if(s[i]=='0')	
				cnt_0++;
			else
				cnt_1++;
		}
	
	if(cnt_0>=1&&cnt_1==0)
	{
		if(t[0]=='1'&&t[m-1]=='1')
		{
			cout<<"YES"<<endl;
			return;
		}
		else
		{
			cout<<"NO"<<endl;
			return;
		}
	}
	else if(cnt_0==0&&cnt_1>=1)
	{
		if(t[0]=='0'&&t[m-1]=='0')
		{
			cout<<"YES"<<endl;
			return;
		}
		else
		{
			cout<<"NO"<<endl;
			return;
		}
	}
	else if(cnt_0==0&&cnt_1==0)
	{
		cout<<"YES"<<endl;
		return;
	}
	else
	{
		cout<<"NO"<<endl;
		return;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	cin>>t;
	
	while(t--)
		solve();
	
	return 0;
}

改成了上面这样,少考虑了本来就是正常的情况,但是还是没有过第二个测试点

思路是,首先判断第一个字符串的长度,特判,假设长度是1,表示一定是好字符串,输出yes

然后判断第二个字符串,如果第二个字符串有相邻的字符是相等的,那么一定不可以使得第一个字符串变成好字符串(好家伙,就是这里出了问题,可以不进行操作)

#include

using namespace std;

void solve()
{
	int n,m;
	cin>>n>>m;
	
	string s,t;
	cin>>s>>t;
	
	if(n==1)
	{
		cout<<"YES"<<endl;
		return;
	}
	
	bool flag=false;
	for(int i=0;i<m-1;i++)
		if(t[i]==t[i+1])
			flag=true;
	
	int cnt_0=0,cnt_1=0;
	for(int i=0;i<n-1;i++)
		if(s[i]==s[i+1])
		{
			if(s[i]=='0')	
				cnt_0++;
			else
				cnt_1++;
		}
	
	if(cnt_0>=1&&cnt_1==0)
	{
		if(t[0]=='1'&&t[m-1]=='1'&&!flag)
		{
			cout<<"YES"<<endl;
			return;
		}
		else
		{
			cout<<"NO"<<endl;
			return;
		}
	}
	else if(cnt_0==0&&cnt_1>=1)
	{
		if(t[0]=='0'&&t[m-1]=='0'&&!flag)
		{
			cout<<"YES"<<endl;
			return;
		}
		else
		{
			cout<<"NO"<<endl;
			return;
		}
	}
	else if(cnt_0==0&&cnt_1==0)
	{
		cout<<"YES"<<endl;
		return;
	}
	else
	{
		cout<<"NO"<<endl;
		return;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	cin>>t;
	
	while(t--)
		solve();
	
	return 0;
}

写着写着博客发现问题所在,第二个字符串只要不使用的话,无论是什么样都不会产生影响

但是如果使用的话,有相邻的字符相等就需要做一个标记,标记为true表示答案一定是no,然后分四种情况来讨论

第一种情况是,第一个字符串有00这种片段并且没有11这种片段,此时要求第二个字符串的标记是false并且首位的字符是11,可以满足条件输出yes,否则输出no

第二种情况是有11没有00,和第一种情况几乎一样

第三种情况是第一个字符串本来就是好字符串,直接输出yes

第四种情况是输出no,就是既有00又有11,无论如何都无法变成好字符串

你可能感兴趣的:(#,CF,div,2,B,题,算法)