USACO SECTION1 1.1 Broken Necklace 暴力枚举(码力)
题目链接:http://train.usaco.org/usacoprob2?a=dyolQaF9eud&S=beads
题目描述: 有b, r, w三种颜色组成的项链儿, 从项链儿一处剪断, 两边分别延伸至不同颜色为止, w可以被当成任意一种颜色
解题思路:暴力枚举, 枚举所有点并且从第一个不是w色的珠子开始, 很麻烦
代码:
/* ID: wl199701 PROG: beads LANG: C++ */ #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <fstream> #include <iterator> #include <map> using namespace std; const int maxn = 370; char a[maxn]; int n; int main() { ofstream fout ("beads.out"); ifstream fin ("beads.in"); while( fin >> n ) { memset(a, 0, sizeof(a)); for( int i = 0; i < n; i++ ) { fin >> a[i]; } int res = -1; for( int i = 0; i < n; i++ ) { int temp = 0; int cnt1 = 1; int h = i; while( a[h] == 'w' ) { cnt1++; h++; if( h == n ) h = 0; if( cnt1 == n ) break; } int j = h + 1; if( j == n ) j = 0; while( 1 ) { if( cnt1 == n ) break; if( j == n ) j = 0; if( a[j] == 'w' ) { cnt1++; j++; continue; } if( a[j] == a[h] ) { cnt1++; j++; } else break; } if( cnt1 == n ) { res = n; break; } int cnt2 = 1; int tempi = i - 1; if( tempi == -1 ) tempi = n-1; while( a[tempi] == 'w' && cnt1 + cnt2 <= n ) { cnt2++; tempi--; if( tempi == -1 ) tempi = n-1; } int k = tempi - 1; if( k == -1 ) k = n-1; while( cnt1 + cnt2 <= n ) { if( a[k] == 'w' ) { cnt1++; k--; if( k == -1 ) k = n-1; continue; } if( a[k] == a[tempi] ) { cnt2++; k--; if( k == -1 ) k = n-1; } else break; } temp = cnt1 + cnt2; if( temp > n ) { temp = n; } if( temp > res ) { res = temp; } } fout << res << endl; } return 0; }
思考:调了快两个点儿, 代码能力还是不行, 还是应该静下心来靠着USACO这个平台练习自己的代码能力, 然后学线段树, dp, 组合数学
posted on 2017-06-01 12:30 FriskyPuppy 阅读(228) 评论(0) 编辑 收藏 举报