# 编程珠玑

## 1.问题描述

`               1 2                               1 2           r b b r                           b r r b          r         b                       b         b         r           r                     b           r        r             r                   w             r       b               r                 w               w      b                 b               r                 r      b                 b               b                 b      b                 b               r                 b       r               r                 b               r        b             r                   r             r         b           r                     r           r           r       r                         r       b             r b r                            r r w          Figure A                     Figure B                        r red bead                        b blue bead                        w white bead`
`项链上的第一个和第二个珠子已经在图中标出了。`

### 输入样例

`29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb`

`11`

## 2.代码实现

import java.util.ArrayList;
import java.util.List;
/**
* 关于一道珠玑算法的实现
* @author guobo 2009-3-17
* @version 1.0
*/
public class GemCode
{
private static final String COLOR_B = "b";
private static final String COLOR_R = "r";
private static final String COLOR_W = "w";
/**
* @param String str 表示珠子颜色的字符串
* @return int[] 返回对该字符串进行统计后的数据数组
* 解析项链的字符串,2个颜色的
* 例如:
* 输入的字符串:brbrrrbbbrrrrrbrrbbrbbbbrrrrb
* 初步转换的字符串:rbrrrbbbrrrrrbrrbbrbbbbrrrrbb
* 最终的结果int[]中放的元素为:{1,1,3,3,5,1,2,2,1,4,4,2}
*/
public int[] transStr(int len, String str)
{
/* 剔除字符串中的空白 */
String tmp = str.replaceAll("\\s", "");

/* 判断输入的字符长度和字符串长度是否一致 */
if(len != tmp.length())
{
System.out.println("the string length is error");
return null;
}

/* 如果输入的字符串有问题,直接返回null */
if (!validit(tmp))
{
System.out.println("intput the string ly user char 'b/r/w'!");
return null;
}
List retList = new ArrayList();

/* 从首个字符开始找起,查找字母发生变化的位置 */
int loc = 0;
if(COLOR_B.equals(tmp.substring(0, 1)))
{
loc = tmp.indexOf(COLOR_R);
}
else
{
loc = tmp.indexOf(COLOR_B);
}
/* 进行第一步转换 */
tmp = tmp.substring(loc) + tmp.substring(0,loc);

/* 获取需要返回的字符串 */
while(tmp.length() > 0)
{
if(COLOR_B.equals(tmp.substring(0, 1)))
{
loc = tmp.indexOf(COLOR_R);
}
else
{
loc = tmp.indexOf(COLOR_B);
}
if(0 <= loc)
{
tmp = tmp.substring(loc);
}
else
{
tmp = "";
}
}
return strToArray(retList);
}

/**
* @param int[] 整型数组
* @return int 返回相邻2个数相加的最大值
* 从生成的数据数组中求相邻2个数相加最大的值
*/
public int getLongNum(int[] data)
{
int len = data.length;

int ret = data[0] + data[len-1];

for (int i = 0; i < len-1; i++)
{
if(ret < data[i] + data[i+1])
{
ret = data[i] + data[i+1];
}
}
return ret;
}

/**
* @param List 输入的列表
* @return int[] 转换后的整形数组
* 将字符串List转换成int[]
*/
public int[] strToArray(List dataList)
{
int len = dataList.size();
int[] ret = new int[dataList.size()];
String elem = "";
for (int i = 0; i < len; i++)
{
elem = (String) dataList.get(i);
try
{
ret[i] = Integer.parseInt(elem);
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
}
return ret;
}

/**
* 字符串校验函数
*/
public boolean validit(String str)
{
/* 确认字符串由字母b/r和空格组成 */
return str.matches("(b|r)*");
}

/**
* 主函数
*/
public static void main(String[] args)
{
GemCode gc = new GemCode();
int len = 29;
String str = "brbrrrbbrrrrrbrrbbrbbbbrwwrrb";

/* 对于有w字符的字符串进行处理 */
if(0 <=str.indexOf(COLOR_W))
{
int a1 = 0;
int a2 = 0;
String str1 = str.replaceAll(COLOR_W, COLOR_R);
if(null !=(gc.transStr(len, str1)))
{
a1 = gc.getLongNum(gc.transStr(len, str1));
}
else
{
return;
}
String str2 = str.replaceAll(COLOR_W, COLOR_B);
if(null !=(gc.transStr(len, str2)))
{
a2 = gc.getLongNum(gc.transStr(len, str2));
}
else
{
return;
}

int ret = a1 > a2 ? a1:a2;
System.out.println(ret);
}
else /* 没有w字符的字符串处理 */
{
if (null !=(gc.transStr(len, str)))
{
System.out.println(gc.getLongNum(gc.transStr(len, str)));
}
}
}
}