六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 26|回复: 0

【水题】USACO Broken Necklace

[复制链接]

升级  68.4%

272

主题

272

主题

272

主题

进士

Rank: 4

积分
842
 楼主| 发表于 2013-1-26 13:31:26 | 显示全部楼层 |阅读模式
进入USACO要注册才可看题: http://train.usaco.org/usacogate

题目:【已翻译】http://www.wzoi.org/usaco/11%5C202.asp

SAMPLE INPUT (file beads.in)
29
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

SAMPLE OUTPUT (file beads.out)
11


又一水题……YY的算法//无法用语言表达啊

……感觉飞一般的繁琐!
虽然1次A……但是写代码时感觉很是不爽


/*ID: 1006100071PROG: beadsLANG: 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-'a' << endl;if (s != 'w' && !hash[s-'a'])memset (hash, false, sizeof(hash)), hash[s-'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;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表