【USACO 1.1.4】破碎的项链
【题目描述】
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
【输入格式】
【输出格式】
【分析】
对于这道题,我只能说,细心,再细心。
1、注意断裂的项链两边有白色的情况。
2、拉链成环时注意已经访问过的不能再次计数。
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
const int maxn=;
using namespace std;
int n,data[maxn*],i,vis[maxn*];
char str[maxn];
int main()
{
//文件操作
freopen("beads.in","r",stdin);
freopen("beads.out","w",stdout);
memset(data,,sizeof(data));
scanf("%d",&n);
scanf("%s",str);
for (i=;i<n;++i)//处理字符串
{
int temp;
if (str[i]=='w') temp=;//白
else if (str[i]=='b') temp=;//蓝
else temp=;//红
data[i+]=data[i+n+]=data[i+n*+]=temp;//拉环成链
}
int ans=,lj,color,point;
for (i=n+;i<=n*;++i)//在i与i+1之间打破
{
lj=;color=data[i];point=i;//指针
//防止重复计数
while (color== && point>=) color=data[--point];
point=i+;
memset(vis,,sizeof(vis));
while (point>)//左边
{
--point;
if (vis[point]) continue;
if (data[point]==color || data[point]==) ++lj;else break;
//打上标记,避免重复计数
int temp=point%n;if (temp==) temp=n;
vis[temp]=vis[temp+n]=vis[temp+*n]=;
}
color=data[i+];point=i+;//指针
while (color== && point<=*n) color=data[++point];
point=i;
while (point<*n)
{
++point;
if (vis[point]) continue;
if (data[point]==color || data[point]==) ++lj;else break;
//打上标记,避免重复计数
int temp=point%n;if (temp==) temp=n;
vis[temp]=vis[temp+n]=vis[temp+*n]=;
}
ans=max(ans,lj);
}
printf("%d",ans);
return ;
}
【USACO 1.1.4】破碎的项链的更多相关文章
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
- USACO翻译:USACO 2012 JAN三题(2)
USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...
- USACO翻译:USACO 2012 JAN三题(1)
USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...
随机推荐
- Maven的常用命令
转载:http://www.cnblogs.com/phoebus0501/archive/2011/05/10/2042511.html Maven库: http://repo2.maven.org ...
- C 函数原型
int add(int,int);//add two int numbers and return it--- add function prototype; int main(int argc, c ...
- 构造函数语义学之Copy Constructor构建操作(1)
一.Copy Constructor的构建操作 就像 default constructor 一样,如果class没有申明一个 copy constructor,就会隐含的声明或隐含的定义一个.生成的 ...
- appendGrid
appendGrid appendGrid的使用
- LVS+Keepalived+Nginx+Tomcat高可用负载均衡集群配置(DR模式,一个VIP,多个端口)
一.概述 LVS作用:实现负载均衡 Keepalived作用:监控集群系统中各个服务节点的状态,HA cluster. 配置LVS有两种方式: 1. 通过ipvsadm命令行方式配置 2. 通过Red ...
- 数据结构(线段树):CodeForces 145E Lucky Queries
E. Lucky Queries time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- 如何在不影响数据库的正常使用的情况下得到数据的完整.mdf和.ldf文
一:完整备份数据库 二:还原数据库 四:分离数据库即可得到.mdf和.ldf文件
- poj1611 并查集
题目链接:http://poj.org/problem?id=1611 #include <cstdio> #include <cmath> #include <algo ...
- Jenkins 十: 访问控制
1. 打开“系统管理” –> “Configure Global Security”. 2. 选中“启用安全”. 3. 找到“安全域”,选中“Jenkins专有数据库”,选中“允许用户注册”. ...
- Let it Bead
http://poj.org/problem?id=2409 // File Name: poj2409.cpp // Author: bo_jwolf // Created Time: 2013年1 ...