【水题】USACO Broken Necklace

[size=small]进入USACO要注册才可看题:[/size] [url]http://train.usaco.org/usacogate[/url]

[size=medium]题目:【已翻译】[url]http://www.wzoi.org/usaco/11%5C202.asp[/url]

[color=blue]SAMPLE INPUT (file beads.in)
29
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

SAMPLE OUTPUT (file beads.out)
11[/color]

又一水题……YY的算法//无法用语言表达啊
[img]http://dl.iteye.com/upload/attachment/494721/fbe1ccf6-29de-31d2-a7d6-e2d6e1e93213.gif[/img]
……感觉飞一般的繁琐!
虽然1次A……但是写代码时感觉很是不爽
[/size]


/*
ID: 1006100071
PROG: beads
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <utility>
#include <iomanip>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

int main()
{
freopen ("beads.in", "r", stdin);
freopen ("beads.out", "w", stdout);
int i, len, maxs, start, change;
bool hash[30] = {0};
string s;
cin >> len >> s;
s = s + s; //实现首尾相接
maxs = 1, change = start = 0;
len <<= 1; //串长加倍
if (s[0] != 'w')
hash[s[0]-'a'] = true, change = 1;
for (i = 1; i < len; i++)
{
//cout << i << ' ' << change << ' ' << s[i]-'a' << endl;
if (s[i] != 'w' && !hash[s[i]-'a'])
memset (hash, false, sizeof(hash)), hash[s[i]-'a'] = true, change++;
if (change > 2 || i == len-1)
{
//cout << i - start << "-----------\n";
if (maxs < i - start)
maxs = i - start;
change = 0;
memset (hash, false, sizeof(hash));
i = start;
start++;
if (s[start] != 'w')
hash[s[start]-'a'] = true, change = 1;
}
}
if (maxs > len / 2) //最多取所给原来的串长
maxs = len / 2;
cout << maxs << endl;
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值