洛谷P1203[USACO1.1]坏掉的项链Broken Necklace

洛谷P1203[USACO1.1]坏掉的项链Broken Necklace

思路:

看起来很简单,枚举每个点两边暴力跑。但是一直wa3和7。因为需要考虑左右第一个遇到的珠子是w的情况。

代码:

#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define cl(x) memset(x,0,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;
string s;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n,i,ans=0;
	cin>>n>>s;
	for(i=1;i<n;i++)
	{
		int a=0,b=0,p=i;
		char c=s[i]; 
		while(s[p]==c || s[p]=='w')
		{
			a++;
			p=(p+1)%n;
			if(c=='w' && s[p]!='w')
				c=s[p];
			if(p==i)
				break;
		}
		p=i-1;
		c=s[i-1];
		while(s[p]==c || s[p]=='w')
		{
			b++;
			p--;
			if(c=='w' && s[p]!='w')
				c=s[p];
			if(p<0)
				p=n-1;
			if(p==i-1)
				break;
		}
		ans=max(ans,a+b);
	}
	ans=min(ans,n);
	cout<<ans<<endl;
	return 0;
}

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

猜你喜欢

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