【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 ...
随机推荐
- 【HDOJ】1525 Euclid's Game
自己想明白的第一道博弈.首先a==b的时候肯定是先手赢: 然后当a>=2*b时,不妨假设a=nb+k, k<b,因此,不论后续怎么博弈,一定可以出现a=k, b=b的情况.因此,无论这个局 ...
- centos qt5,PyQt5 installation
一.SIP http://www.riverbankcomputing.com/software/sip/download 二.Centos6.5 qt 安装 1,centos linux系统必须 ...
- weblogic jsp 不生效解决方法
1. 检查weblogic.xml配置文件,其中如果有: <jsp-descriptor> <jsp-param> <param-name>pageCheckSec ...
- 数据结构,可并堆(左偏树):COGS [APIO2012] 派遣
796. [APIO2012] 派遣 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿. 在这个帮派里,有一名忍者被称之为Master.除了Master以外,每名忍者都有且 ...
- 【模拟】Codeforces 704A & 705C Thor
题目链接: http://codeforces.com/problemset/problem/704/A http://codeforces.com/problemset/problem/705/C ...
- Invert Binary Tree——LeetCode
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- cf703B Mishka and trip
B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Uncle Sam 山姆大叔
山姆大叔被用来代指“美国”或“美国政府”,主要在美国.英国,尤其是在新闻界中使用较多.“山姆大叔”是美国的绰号,它同自由女神一样,为世人所熟知. 形象 美国的报纸杂志.文学作品和漫画中,经常可以看到“ ...
- 《A First Course in Mathematical Modeling》-chaper1-差分方程建模
从今天开始笔者将通过这个专栏可是对“数学建模”的学习.其实对于“数学建模”自身的内涵或者意义并不需要太多的阐释,下图简洁明了的阐释了数学建模的意义. 其实数学建模本身可以看成换一种角度去解读数学,将我 ...
- SoupUI学习资料
官网: https://www.soapui.org 下载地址: https://www.soapui.org/downloads/soapui.html 官方文档: https://www.soap ...