USACO 1.2 Broken Necklace
断点是白色的情况在做题的时候完全没有想到呢...
看到了数据才发现这个问题$qwq$
1 /* 2 ID:Starry21 3 LANG:C++ 4 TASK:beads 5 */ 6 #include<iostream> 7 #include<string> 8 #include<cstdio> 9 #include<cstring> 10 #include<map> 11 using namespace std; 12 #define N 355 13 int n,ans; 14 char s[N*2]; 15 int l,r; 16 int f2(char c) 17 { 18 int res=0; 19 while(1) 20 { 21 if(s[l+1]==c||s[l+1]=='w') 22 l++,res++; 23 else break; 24 if(l+1==r) break; 25 } 26 return res; 27 } 28 int f1() 29 { 30 int res1=0,res2=0; 31 int ll=l;char c='b'; 32 while(1) 33 { 34 if(s[l+1]==c||s[l+1]=='w') 35 l++,res1++; 36 else break; 37 if(l+1==r) break; 38 } 39 c='r'; 40 while(1) 41 { 42 if(s[ll+1]==c||s[ll+1]=='w') 43 ll++,res2++; 44 else break; 45 if(ll+1==r) break; 46 } 47 if(res2>res1) l=ll; 48 return max(res1,res2); 49 } 50 int f4(char c) 51 { 52 int res=0; 53 while(1) 54 { 55 if(s[r-1]==c||s[r-1]=='w') 56 r--,res++; 57 else break; 58 if(l+1==r) break; 59 } 60 return res; 61 } 62 int f3() 63 { 64 int res1=0,res2=0; 65 int rr=r;char c='b'; 66 while(1) 67 { 68 if(s[r-1]==c||s[r-1]=='w') 69 r--,res1++; 70 else break; 71 if(l+1==r) break; 72 } 73 c='r'; 74 while(1) 75 { 76 if(s[rr-1]==c||s[rr-1]=='w') 77 rr--,res2++; 78 else break; 79 if(l+1==rr) break; 80 } 81 if(res2>res1) r=rr; 82 return max(res1,res2); 83 } 84 int main() 85 { 86 //freopen("beads.in","r",stdin); 87 //freopen("beads.out","w",stdout); 88 scanf("%d",&n); 89 scanf("%s",s+1); 90 for(int i=1;i<=n;i++) 91 s[i+n]=s[i]; 92 for(int i=1;i<=n;i++) 93 { 94 l=i,r=i+n-1; 95 int cnt1=1,cnt2=1; 96 char c=s[l]; 97 if(c!='w') cnt1+=f2(c); 98 else cnt1+=f1(); 99 if(l+1==r) 100 { 101 ans=max(ans,cnt1+1/*还要加上端点r*/); 102 continue; 103 } 104 //---- 105 c=s[r]; 106 if(c!='w') cnt1+=f4(c); 107 else cnt1+=f3(); 108 ans=max(ans,cnt1+cnt2); 109 } 110 printf("%d\n",ans); 111 return 0; 112 } 113 /* 114 77 115 rwrwrwrwrwrwrwrwrwrwrwrwbwrwbwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwr 116 */
转载请注明出处,有疑问欢迎探讨
博主邮箱 2775182058@qq.com