USACO 之 Section 1.2 Complete Search (已解决)

Milking Cows :  对输入的工作时间做排序处理,之后模拟工作过程,求出答案。。

 1 /*
 2 ID: Jming
 3 PROG: milk2
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdlib>
 8 #include <cstdio>
 9 #include <algorithm>
10 using namespace std;
11 const int MAX_N = 5005;
12 int N;
13 struct node {
14     int a, b;
15 };
16 
17 node Time[MAX_N];
18 
19 bool Cmp(const node &n1, const node &n2) {
20     if (n1.a == n2.a) return n1.b < n2.b;
21     else return n1.a < n2.a;
22 }
23 
24 int main()
25 {
26     freopen("milk2.in", "r", stdin);
27         freopen("milk2.out", "w", stdout);
28     scanf("%d", &N);
29     for (int i = 0; i < N; ++i) {
30         scanf("%d %d", &Time[i].a, &Time[i].b);
31     }
32     sort(Time, Time+N, Cmp);
33     int mybegin = Time[0].a, myend = Time[0].b;
34     int work = myend - mybegin, no_work = 0;
35     for (int i = 1; i < N; ++i) {
36         if (Time[i].a <= myend) {
37             myend = max(myend, Time[i].b);
38         }else {
39             work = max(work, myend - mybegin);
40             no_work = max(no_work, Time[i].a - myend);
41             mybegin = Time[i].a, myend = Time[i].b;
42         }
43     }
44     work = max(work, myend - mybegin);
45     printf("%d %d\n", work, no_work);
46     return 0;
47 }

 

Transformations :将转换方法在纸上模拟一下,可发现规律,之后变成程序处理即可。。。

  1 /*
  2 ID: Jming
  3 PROG: transform
  4 LANG: C++
  5 */
  6 #include <iostream>
  7 #include <cstdlib>
  8 #include <cstdio>
  9 #include <cstring>
 10 using namespace std;
 11 const int MAX_N = 15;
 12 char arr_str[MAX_N][MAX_N], t_arr_str[MAX_N][MAX_N], to_arr_str[MAX_N][MAX_N];
 13 int N;
 14 
 15 bool Judge(char str1[][15], char str2[][15]) {
 16     for (int i = 0; i < N; ++i) {
 17         for (int j = 0; j < N; ++j) {
 18             if (str1[i][j] != str2[i][j]) {
 19                 return false;
 20             }
 21         }
 22     }
 23     return true;
 24 }
 25 
 26 void number1(char str1[][15], char str2[][15]) {
 27     for (int i = 0; i < N; ++i) {
 28         for (int j = 0; j < N; ++j) {
 29             str1[j][N - 1 - i] = str2[i][j];
 30         }
 31     }
 32 }
 33 
 34 void number2(char str1[][15], char str2[][15]) {
 35     for (int i = 0; i < N; ++i) {
 36         for (int j = 0; j < N; ++j) {
 37             str1[N - 1 - i][N - 1 - j] = str2[i][j];
 38         }
 39     }
 40 }
 41 
 42 void number3(char str1[][15], char str2[][15]) {
 43     for (int i = 0; i < N; ++i) {
 44         for (int j = 0; j < N; ++j) {
 45             str1[N - 1 - j][i] = str2[i][j];
 46         }
 47     }
 48 }
 49 
 50 void number4(char str1[][15], char str2[][15]) {
 51     for (int i = 0; i < N; ++i) {
 52         for (int j = 0; j < N; ++j) {
 53             str1[i][N - 1 - j] = str2[i][j];
 54         }
 55     }
 56 }
 57 
 58 void Solve() {
 59     number1(t_arr_str, arr_str);
 60     if (Judge(t_arr_str, to_arr_str)) {
 61         printf("1\n");
 62         return;
 63     }
 64 
 65     number2(t_arr_str, arr_str);
 66     if (Judge(t_arr_str, to_arr_str)) {
 67         printf("2\n");
 68         return;
 69     }
 70 
 71     number3(t_arr_str, arr_str);
 72     if (Judge(t_arr_str, to_arr_str)) {
 73         printf("3\n");
 74         return;
 75     }
 76 
 77 
 78     number4(t_arr_str, arr_str);
 79     if (Judge(t_arr_str, to_arr_str)) {
 80         printf("4\n");
 81         return;
 82     }
 83 
 84 
 85     char arr_str1[MAX_N][MAX_N];
 86     for (int i = 0; i < N; ++i) {
 87         strcpy(arr_str1[i], t_arr_str[i]);
 88     }
 89 
 90     number1(t_arr_str, arr_str1);
 91     if (Judge(t_arr_str, to_arr_str)) {
 92         printf("5\n");
 93         return;
 94     }
 95 
 96     number2(t_arr_str, arr_str1);
 97     if (Judge(t_arr_str, to_arr_str)) {
 98         printf("5\n");
 99         return;
100     }
101 
102     number3(t_arr_str, arr_str1);
103     if (Judge(t_arr_str, to_arr_str)) {
104         printf("5\n");
105         return;
106     }
107     if (Judge(t_arr_str, to_arr_str)) {
108         printf("6\n");
109         return;
110     }
111     printf("7\n");
112 }
113 
114 int main()
115 {
116     freopen("transform.in", "r", stdin);
117     freopen("transform.out", "w", stdout);
118     scanf("%d", &N);
119     for (int i = 0; i < N; ++i) {
120         scanf("%s", arr_str[i]);
121         strcpy(t_arr_str[i], arr_str[i]);
122     }
123     for (int i = 0; i < N; ++i) {
124         scanf("%s", to_arr_str[i]);
125     }
126     Solve();
127     return 0;
128 }

 

Name That Number :字典树 + Dfs  (解法同  hdu 1298  解题报告点击此处

 1 /*
 2 ID: Jming
 3 PROG: namenum
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdlib>
 8 #include <cstdio>
 9 #include <fstream>
10 #include <string>
11 #include <cstring>
12 #include <map>
13 using namespace std;
14 const int MAX = 26;
15 map<int, string> myMap;
16 bool ansJudge;
17 
18 struct Trie{
19     bool Judge;
20     Trie* next[MAX];
21     Trie() {
22         Judge = false;
23         memset(next, NULL, sizeof(next));
24     }
25 };
26 
27 Trie* Root;
28 
29 void CreTrie(string str) {
30     int len = str.size();
31     Trie* p = Root;
32     for (int i = 0; i < len; ++i) {
33         int pos = str[i] - 'A';
34         if (!(p->next[pos])) {
35             p->next[pos] = new Trie;
36         }
37         p = p->next[pos];
38     }
39     p->Judge = true;
40 }
41 
42 void DelTrie(Trie* T) {
43     for (int i = 0; i < MAX; ++i) {
44         if (T->next[i]) {
45             DelTrie(T->next[i]);
46         }
47     }
48     delete[] T;
49 }
50 
51 void Ini() {
52     ifstream fcin("dict.txt");
53     if (!fcin) {
54         system("pause");
55     }
56     string str;
57     while (fcin >> str) {
58         CreTrie(str);
59     }
60     myMap[2] = "ABC"; myMap[3] = "DEF"; myMap[4] = "GHI"; myMap[5] = "JKL";
61     myMap[6] = "MNO"; myMap[7] = "PRS"; myMap[8] = "TUV"; myMap[9] = "WXY";
62 }
63 
64 void Dfs(string str, int pos, Trie* T, string ansStr) {
65     for (int i = 0; i < 3; ++i) {
66         Trie* t_Trie = T->next[myMap[str[pos] - '0'][i] - 'A'];
67         if (t_Trie) {
68             if ((t_Trie->Judge) && (pos == (str.size() - 1))) {
69                 if (!ansJudge) ansJudge = true;
70                 cout << ansStr + myMap[str[pos] - '0'][i] << endl;
71             }
72             else Dfs(str, pos + 1, t_Trie, ansStr + myMap[str[pos] - '0'][i]);
73         }
74     }
75 }
76 
77 int main()
78 {
79    Root = new Trie;
80    ansJudge = false;
81     Ini();
82     freopen("namenum.in", "r", stdin);
83     freopen("namenum.out", "w", stdout);
84     string str;
85     cin >> str;
86     Dfs(str, 0, Root, "");
87     if (!ansJudge) {
88         cout << "NONE" << endl;
89     }
90     DelTrie(Root);
91     return 0;
92 }

 


Palindromic Squares :进制处理。。。

 1 /*
 2 ID: Jming
 3 PROG: palsquare
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdlib>
 8 #include <cstdio>
 9 #include <string>
10 using namespace std;
11 int base;
12 
13 struct myNode {
14     int number;
15     int square;
16 }node[305];
17 
18 void Init() {
19     for (int i = 1; i <= 300; ++i) {
20         node[i - 1].number = i;
21         node[i - 1].square = i * i;
22     }
23 }
24 
25 char judgeChar(int tmp) {
26     if (tmp >= 0 && tmp <= 9) return ('0' + tmp);
27     else return ('A' + (tmp - 10));
28 }
29 
30 string getNum(int x) {
31     string str = "";
32     while (x >= base) {
33         int tmp = x%base;
34         x /= base;
35         str = judgeChar(tmp) + str;
36     }
37     return (judgeChar(x) + str);
38 }
39 
40 bool judgePalindromic(string str) {
41     int myBegin = 0, myEnd = str.size() - 1, myCount = str.size() >> 1;
42     for (int i = 0; i < myCount; ++i) {
43         if (str[myBegin + i] != str[myEnd - i]) {
44             return false;
45         }
46     }
47     return true;
48 }
49 
50 void Solve() {
51     for (int i = 0; i < 300; ++i) {
52         string str = getNum(node[i].square);
53         if (judgePalindromic(str)) {
54             cout << (getNum(node[i].number)) << " " << str << endl;
55         }
56     }
57 }
58 
59 int main()
60 {
61     freopen("palsquare.in", "r", stdin);
62     freopen("palsquare.out", "w", stdout);
63     Init();
64     scanf("%d", &base);
65     Solve();
66     return 0;
67 }

 

Dual Palindromes :进制处理。。。

 1 /*
 2 ID: Jming
 3 PROG: dualpal
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdlib>
 8 #include <cstdio>
 9 #include <string>
10 using namespace std;
11 int base, N, S;
12 
13 string getNum(int x) {
14     string str = "";
15     while (x >= base) {
16         int tmp = x%base;
17         x /= base;
18         str = (char)('0' + tmp) + str;
19     }
20     return ((char)('0' + x) + str);
21 }
22 
23 bool judgePalindromic(string str) {
24     int myBegin = 0, myEnd = str.size() - 1, myCount = str.size() >> 1;
25     for (int i = 0; i < myCount; ++i) {
26         if (str[myBegin + i] != str[myEnd - i]) {
27             return false;
28         }
29     }
30     return true;
31 }
32 
33 void Solve() {
34     int myCount = 1;
35     while (myCount <= N) {
36         ++S;
37         int sum = 0;
38         for (base = 2; base <= 10; ++base) {
39             string str = getNum(S);
40             if (judgePalindromic(str)) {
41                 ++sum;
42                 if (sum >= 2) break;
43             }
44         }
45         if (sum >= 2) {
46             printf("%d\n", S);
47             ++myCount;
48         }
49     }
50 }
51 
52 int main()
53 {
54     freopen("dualpal.in", "r", stdin);
55     freopen("dualpal.out", "w", stdout);
56     scanf("%d %d", &N, &S);
57     Solve();
58     return 0;
59 }

 

posted @ 2014-09-19 23:06  JmingS  阅读(169)  评论(0编辑  收藏  举报