当前位置: 代码迷 >> 综合 >> USCAO-Section1.1 Broken Necklace(暴力枚举版dp版待更新)
  详细解决方案

USCAO-Section1.1 Broken Necklace(暴力枚举版dp版待更新)

热度:117   发布时间:2023-09-19 12:22:02.0

原题:
You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29:

            1 2                               1 2r b b r                           b r r br         b                       b         br           r                     b           rr             r                   w             rb               r                 w               wb                 b               r                 rb                 b               b                 bb                 b               r                 br               r                 b               rb             r                   r             rb           r                     r           rr       r                         r       br b r                             r r wFigure A                         Figure Br red beadb blue beadw white bead

The beads considered first and second in the text that follows have been marked in the picture.

The configuration in Figure A may be represented as a string of b’s and r’s, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .

Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).

Determine the point where the necklace should be broken so that the most number of beads can be collected.

题意:
有一串链子,由三种颜色的珠子组成:红r 、蓝b 、白w(可以当做红色也可以当做蓝色),从某一处断开,使得从断开处顺时针和逆时针分别取珠子,若碰到第一个不同色的珠子就停止,求最多取多少个珠子。
输入要求:
n(n<=400)字符串长度
str (只包含小写的[wbr])字符串
题解:
其实就是一个字符串,首尾相连,求从某两个字符串中间开始往左右遍历,遇到非w和不同色的就停止,找出最多的可以连在一起的字符串的长度。
其中需要考虑的有:
1、珠子一共这么多,顺时针逆时针碰头要停止。
2、开始遍历的字符如果是w要进行处理,找到非w的字符以进行不同色就停止的判断。

详细在代码中解释:

/* ID:newyear111 PROG: beads LANG: C++ */
/* 暴力枚举 */
#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
using namespace std;
const int N=400;int n;
char str[N];
//求从下标为node处的字符的左侧断开所取的珠子的数量 
int sumOfBeads(int node)
{//sum计数 cnt控制取珠子时顺逆时针碰头后停止(即珠子取完) int sum=0;int cnt=0;//使用取模运算模拟环 int i=(node)%n;int j=(node-1+n)%n;//c1记录向右第一个不是w的珠子的颜色 反之c2记录向左第一个不是w的珠子的颜色 char c1,c2;//找出向右的第一个不是w的珠子 注意用cnt控制可取珠子的数量 while(str[i]=='w'&&cnt<n){sum++;i=(i+1)%n;cnt++;}c1=str[i];//向左的 while(str[j]=='w'&&cnt<n){sum++;j=(j+n-1)%n;cnt++;}c2=str[j];//下面是向左和向右取珠子的模拟 while(cnt<n){if(str[i]==c1||str[i]=='w'){sum++;cnt++;i=(i+1)%n;  }   elsebreak;}   while(cnt<n){if(str[j]==c2||str[j]=='w'){sum++;cnt++;j=(j-1+n)%n;    }   elsebreak;}//返回sum的值 return sum;
} 
int main()
{ifstream fin("beads.in");ofstream fout("beads.out");fin>>n;fin>>str;int ans=0;//在每一个珠子左侧断开求取得珠子数,与ans比较 for(int i=0;i<n;i++){ans=max(ans,sumOfBeads(i));}fout<<ans<<endl;fin.close();fout.close();return 0;
}