Simple Complete Search

1. Broken Necklace

  This is my solution to the USACO training problem "beads":

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class beads {
 5     public static BufferedReader input;
 6     public static PrintWriter output;
 7     public static int len;
 8     public static String str;
 9     
10     public static boolean same(int i,int j) {
11         char a=str.charAt(i), b=str.charAt(j);
12         if (a=='w' || b=='w') {
13             return true;
14         } else {
15             return a==b;
16         }
17     }
18     public static boolean diff(int i,int j) {
19         char a=str.charAt(i),b=str.charAt(j);
20         if (a=='w' || b=='w') {
21             return true;
22         } else {
23             return a!=b;
24         }
25     }
26     public static int searchLeft(int pos) {
27         int val = 0;
28         for (int i=1;i<len;i++) {
29             if (!diff(pos,(pos+len-i)%len)) {
30                 break;
31             } else {
32                 val++;
33             }
34         }
35         return val;
36     }
37     public static int searchRight(int pos) {
38         int val = 1;
39         for (int i=1;i<len;i++) {
40             if (!same(pos,(pos+i)%len)) {
41                 break;
42             } else {
43                 val++;
44             }
45         }
46         return val;
47     }
48     public static int solve() {
49         int val = 0;
50         for (int i=0;i<len;i++) {
51             // For every bead i whose color is red or blue:
52             //        traverse its right side until an opposite color appears;
53             //        traverse its left side until a same color shows up
54             if (str.charAt(i)=='w') {
55                 continue;
56             }
57             int tmp = searchLeft(i);
58             tmp += searchRight(i);
59             if (tmp>val) {
60                 val = tmp;
61             }
62         }
63         if (val==0||val>len) {
64             // probably all beads are white
65             return len;
66         } else {
67             return val;
68         }
69     }
70     public static void main(String[] args) throws IOException{
71         input = new BufferedReader(new FileReader("beads.in"));
72         len = Integer.parseInt(input.readLine());
73         str = input.readLine();
74         input.close();
75         output = new PrintWriter(new FileWriter("beads.out"));
76         output.println(solve());
77         output.close();
78     }
79 }

2. Milking Cows

  This is my solution to the USACO training problem "milk2":

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class milk2 {
 5     // KEEP IT SIMPLE AND STUDPID
 6     
 7     public static final int MAX=1000001;
 8     public static BufferedReader input;
 9     public static PrintWriter output;
10     public static boolean [] busy;
11     
12     public static void solve(int start,int end) {
13         int milk=0, free=0, mkTmp=0, frTmp=0;
14         for (int i=start;i<=end;i++) {
15             if (busy[i] && mkTmp++==0) {
16                 if (frTmp>free) {
17                     free = frTmp;
18                 }
19                 frTmp = 0;
20             } else if (!busy[i] && frTmp++==0){
21                 if (mkTmp>milk) {
22                     milk = mkTmp;
23                 }
24                 mkTmp = 0;
25             }
26         }
27         output.println(milk+" "+free);
28     }
29     public static void main(String[] args) throws IOException{
30         input = new BufferedReader(new FileReader("milk2.in"));
31         int n = Integer.parseInt(input.readLine());
32         busy = new boolean [MAX];
33         int start=MAX, end=0;
34         for (int i=0;i<n;i++) {
35             StringTokenizer str = new StringTokenizer(input.readLine());
36             int p = Integer.parseInt(str.nextToken());
37             if (p<start) {
38                 start = p;
39             }
40             int r = Integer.parseInt(str.nextToken());
41             if (r>end) {
42                 end = r;
43             }
44             for (int j=p;j<r;j++) {
45                 busy[j] = true;
46             }
47         }
48         input.close();
49         output = new PrintWriter(new FileWriter("milk2.out"));
50         solve(start,end);
51         output.close();
52     }
53 }

3. Calf Flac

  This is my solution to the USACO training problem "calfflac":

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class calfflac{
 5     public static BufferedReader input;
 6     public static PrintWriter output;
 7     public static char [] data;
 8     public static int [] index;
 9     
10     public static boolean isChar(char ch) {
11         if (ch>='A' && ch<='Z') {
12             return true;
13         } else if (ch>='a'&&ch<='z') {
14             return true;
15         } else {
16             return false;
17         }
18     }
19     public static boolean same(int i,int j) {
20         char a=data[index[i]];
21         char b=data[index[j]];
22         return a==b || a==b+32 || a==b-32;
23     }
24     public static int odd(int pos,int len) {
25         int val = 0;
26         for (;pos-val>=0 && pos+val<len;val++) {
27             if (!same(pos-val,pos+val)) {
28                 break;
29             }
30         }
31         return val-1;
32     }
33     public static int even(int pos,int len)  {
34         int val;
35         for (val=0;pos-val>=0 && pos+val+1<len;val++) {
36             if (!same(pos-val,pos+val+1)) {
37                 break;
38             }
39         }
40         return val-1;
41     }
42     public static void solve(int len) {
43         int max=0, start=0, end=0;
44         for (int i=0;i<len;i++) {
45             int j = odd(i,len);
46             if (max<(j<<1)+1) {
47                 max = (j<<1)+1;
48                 start = i-j;
49                 end = i+j;
50             }
51         }
52         for (int i=0;i<len;i++) {
53             int j = even(i,len);
54             if (max<(j<<1)+2) {
55                 max = (j<<1)+2;
56                 start = i-j;
57                 end = i+j+1;
58             }
59         }
60         output.println(max);
61         for (int i=index[start];i<=index[end];i++) {
62             output.print(data[i]);
63         }
64         output.println();
65     }
66     public static void main(String[] args) throws IOException  {
67         data = new char [20000];
68         index = new int [20000];
69         input = new BufferedReader(new FileReader("calfflac.in"));
70         int pos=0, len=0;
71         String line = input.readLine();
72         while (line!=null)  {
73             for (int i=0;i<line.length();i++) {
74                 data[pos++] = line.charAt(i);
75                 if (isChar(line.charAt(i))) {
76                     index[len++] = pos-1;
77                 }
78             }
79             data[pos++] = '
';
80             line = input.readLine();
81         }
82         input.close();
83         output = new PrintWriter(new FileWriter("calfflac.out"));
84         solve(len);
85         output.close();
86     }
87 }

 

原文地址:https://www.cnblogs.com/DevinZ/p/4411442.html