1、串聯兩條鏈解開環形
2、暴力搜索
3、注意邊界條件
3
rrr
易出錯!
/* ID: gengjia1 LANG: C TASK: beads */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> int main(void) { FILE *fin = fopen ("beads.in", "r"); FILE *fout = fopen ("beads.out", "w"); int N, n = 0; char *s = NULL, *st = NULL; int fh = 0, sh = 0; //first half, second half char fc, lc, cc; //first half char, last char, current char int i, j; fscanf (fin, "%d", &N); assert(N >= 3); assert(N <= 350); s = (char *)malloc(2 * N + 1); st = (char *)malloc(N+1); if( s == NULL || st == NULL) exit(1); fscanf (fin, "%s", st); assert(strlen(st) == N); s = strcpy(s, st); s = strcat(s, st); for(i=0; i < N; i++) { fh = 0; sh = 0; j = 0; fc = lc = s[i]; fh += 1; j = i + 1; cc = s[j]; while( !(sh > 0 && cc == fc) ) //前半段字符第二次出現相等 { if(cc == lc) { if(sh == 0) { if(fh >= N) { break; } fh += 1; } else { sh +=1; } } else { if(fc == lc) { if(fc == 'w') { fh += 1; fc = cc; } else if(cc =='w') { fh += 1; } else { sh += 1; } } else { if(fc == cc) { fh += 1; } else { sh += 1; } } lc = cc; } j += 1; cc = s[j]; } if (n < fh + sh) { n = fh + sh; } if (n == N) { break; //注意邊界條件 } } fprintf (fout, "%d/n", n); free(s); fclose(fin); fclose(fout); exit(0); }
由於這套題某兩題重複了3次,我就只好放在一起寫了 Cow Hopscotch 這道題Bronze,Silver,Gold各有一道,難度依次增加。 對於Bronze的,顏色只有兩種,r和c<=15,O(n^4) 跑一發dp即可(比n
搜索搜索搜索! 每次把一塊積木移動一步,而且每塊積木內部的相對位置是不變的,那麼每次只要記錄積木任意一點的位置表示狀態即可.爲了方便,我們可以設爲每個積木包圍塊的左上角的點. 如何實現? 一種想法是暴搜!可惜T了… 暴
sb題,DP搞不成,解空間太大了。2的31次方 由於數的個數很少,但是解空間很大,加了個剪紙瞎逼搜就過去了 /* ID: daniel.20 LANG: JAVA TASK: tour */ import java.util.*; i
PROB Your Ride Is Here 有些星星上會有UFO來接地球人上去玩,但是這個UFO一次只能有一組人過去。所以要讓這組人知道他們被選中了。方法是這樣的,星星的名字和組名能確定一個小組該去哪兒。然後我們就要寫一個程序,判斷一組
Name That Number Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please
Barn Repair It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows
Transformations A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another
http://usacotraining.blogspot.com/2012/08/problem-333-camelot.html something about github http://blog.sina.com.cn/s/
題目大意 題上的中文題意太不明確了。。。 給出一個拓撲圖,每條有向邊有兩個權值,有兩個人從1出發到n,分別走這兩種權值。問有沒有權值使得這兩個人都能走過這些權值到達n。 思路 看懂了題之後就水了。維護兩個數組表示從1號節點是否能
[題目描述] Packing Rectangles 鋪放矩形塊 IOI 95 給定4個矩形塊,找出一個最小的封閉矩形將這4個矩形塊放入,但不得相互重疊。所謂最小矩形指該矩形面積最小。 所有4個矩形塊的邊都與封閉矩形的邊相平行,圖1示
[題目描述] The Clocks 時鐘 IOI'94 - Day 2 考慮將如此安排在一個 3 x3 行列中的九個時鐘: 目標要找一個最小的移動順序次將所有的指針指向12點。 下面原表格列出了9種不同的旋轉指針的方法,每一種方
//Main idea: //Dynamic Programming //dp[i][j] denote the number of binary trees whose node number is i and height is l
//Main idea //Calculate the shortest path from 'Z' to ohter pastures by Dijkstra algorithm; //And then choose shortest
//Main Idea: //brute force; //shares[i][j] is the initial shares company i own for company j; //fianl_shares[i][j] is t
//Main idea //First we find the two exits and use flood fill to calculate their distance //to any other block;Compare e