1.1.4Broken Necklace

  1. /*
  2. ID: awsd1231
  3. PROG: beads
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. usingnamespace std;
  9. char necklace[500][2]={0};
  10. int num[500]={0};
  11. int n;
  12. void clearNec(){
  13. for(int i =0; i != n; i++)
  14. necklace[i][1]=0;
  15. }
  16. int sumRight(int x){
  17. char a = necklace[x][0];
  18. int t =0;
  19. while(necklace[x+t][0]=='w')
  20. a = necklace[x+++t][0];
  21. int count =0;
  22. for(int i = x; i != n;++i)
  23. if(!necklace[i][1]&&(a == necklace[i][0]|| necklace[i][0]=='w'))++count;
  24. elsereturn count;
  25. for(int i =0; i != x;++i)
  26. if(!necklace[i][1]&&(a == necklace[i][0]|| necklace[i][0]=='w'))++count;
  27. elsereturn count;
  28. return count;
  29. }
  30. int sumLeft(int x){
  31. char a;
  32. if(x) a = necklace[x-1][0];
  33. else a = necklace[n-1][0];
  34. int t =0;
  35. while(necklace[x-t][0]=='w')
  36. a = necklace[x-++t][0];
  37. int count =0;
  38. for(int i = x -1; i !=-1;--i)
  39. if(a == necklace[i][0]|| necklace[i][0]=='w'){
  40. ++count;
  41. necklace[i][1]=1;
  42. }
  43. elsereturn count;
  44. for(int i = n -1; i != x -1;--i)
  45. if(a == necklace[i][0]|| necklace[i][0]=='w'){
  46. ++count;
  47. necklace[i][1]=1;
  48. }
  49. elsereturn count;
  50. return count;
  51. }
  52. int maxSum(){
  53. int max =0;
  54. for(int i =0; i != n;++i)
  55. if(max < num[i]) max = num[i];
  56. return max;
  57. }
  58. int main(){
  59. freopen("beads.in","r", stdin);
  60. freopen("beads.out","w", stdout);
  61. cin >> n;
  62. for(int i =0; i != n;++i)
  63. cin >> necklace[i][0];
  64. for(int i =0; i != n;++i){
  65. clearNec();
  66. num[i]= sumLeft(i)+ sumRight(i);
  67. }
  68. cout << maxSum()<< endl;
  69. return0;
  70. }
 





你可能感兴趣的:(BR)