洛谷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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章