1 HDU 1880 魔咒词典

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #include <stack>
 9 #include <map>
10 #include <set>
11 #include <cmath>
12 #include <cctype>
13 #include <ctime>
14 
15 using namespace std;
16 
17 #define REP(i, n) for (int i = 0; i < (n); ++i)
18 #define eps 1e-9
19 #define PI acos(-1.0)
20 #define lc id << 1
21 #define rc id << 1 | 1
22 #define lson low, mid, lc
23 #define rson mid + 1, high, rc
24 
25 typedef long long ll;
26 typedef pair<int, int> pii;
27 typedef unsigned int uint;
28 
29 const int INF = 0x7fffffff;
30 const int mod = 1e5 + 7;
31 const int maxn = 1e5 + 10;
32 struct Ans { char str1[25], str2[85]; };
33 
34 char opt[120], str_t[2][90];
35 Ans ans[2][maxn];
36 int Next[2][maxn], hash_table[2][maxn], cur[2];
37 int len, n, cnt1, cnt2;
38 
39 uint BKDR_Hash(char *str) {
40     uint hash = 0;
41     while (*str) { hash = hash * 131 + (*str++); }
42     return (hash & 0x7fffffff) % mod;
43 }
44 void insert(int flag) {
45     int t = BKDR_Hash(str_t[flag]);
46     strcpy(ans[flag][cur[flag]].str1, str_t[0]); strcpy(ans[flag][cur[flag]].str2, str_t[1]);
47     Next[flag][cur[flag]] = hash_table[flag][t]; hash_table[flag][t] = cur[flag]++;
48 }
49 inline void print(char *str) {
50     if (str[0] != '[') { printf("%s\n", str); return; } len = strlen(str);
51     for (int i = 1; i < len - 1; ++i) { printf("%c", str[i]); } printf("\n");
52 }
53 
54 int main() {
55 #ifdef __AiR_H
56     freopen("in.txt", "r", stdin);
57 //    freopen("out.txt", "w", stdout);
58 #endif // __AiR_H
59     memset(hash_table, -1, sizeof(hash_table)); int t; bool flag; int key;
60     while (gets(opt) && opt[0] != '@') {
61         len = strlen(opt); cnt1 = cnt2 = 0; int i = 0;
62         memset(str_t, 0, sizeof(str_t));
63         for (; opt[i] != ']'; ++i) { str_t[0][cnt1++] = opt[i]; } str_t[0][cnt1++] = ']';
64         for (i += 2; i < len; ++i) { str_t[1][cnt2++] = opt[i]; }
65         insert(0); insert(1);
66     }
67     scanf("%d", &n); getchar();
68     while (n--) {
69         gets(opt); flag = false; key = 0; if (opt[0] != '[') { key = 1; }
70         t = BKDR_Hash(opt); t = hash_table[key][t];
71         while (t != -1) {
72             if (key == 0) {
73                 if (strcmp(opt, ans[key][t].str1) == 0) {
74                     print(ans[key][t].str2); flag = true; break;
75                 }
76             } else {
77                 if (strcmp(opt, ans[key][t].str2) == 0) {
78                     print(ans[key][t].str1); flag = true; break;
79                 }
80             }
81             t = Next[key][t];
82         }
83         if (!flag) { printf("what?\n"); }
84     }
85 #ifdef __AiR_H
86     printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
87 #endif // __AiR_H
88     return 0;
89 }
View Code

 


1 POJ 3349 Snowflake Snow Snowflakes

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #include <stack>
 9 #include <map>
10 #include <set>
11 #include <cmath>
12 #include <cctype>
13 #include <ctime>
14 
15 using namespace std;
16 
17 typedef long long ll;
18 typedef unsigned int uint;
19 const int maxn = 10;
20 const int mod = 1e5 + 7;
21 int n, cur = 0;
22 int a[maxn], a_t[maxn], s_t[maxn];
23 int key[maxn][mod * 2], Next[mod * 2], head[mod * 2];
24 
25 int get_min(int s[]) {
26     int i = 0, j = 1, k = 0, t;
27     while (i < 6 && j < 6 && k < 6) {
28         t = s[(i + k) % 6] - s[(j + k) % 6];
29         if (!t) { ++k; continue; }
30         if (t > 0) { i += k + 1; } else { j += k + 1; }
31         if (i == j) { ++j; } k = 0;
32     }
33     return min(i, j);
34 }
35 uint cal(int s[]) {
36     int t = get_min(s); uint ret = 0;
37     for (int i = t, j = 0; j < 6; i = (i + 1) % 6, ++j) {
38         ret = ret * 131 + s[i]; s_t[j] = s[i];
39     }
40     for (int i = 0; i < 6; ++i) { s[i] = s_t[i]; }
41     return ret % mod;
42 }
43 void insert(int x, int s[]) {
44     for (int i = 0; i < 6; ++i) { key[i][cur] = s[i]; }
45     Next[cur] = head[x]; head[x] = cur++;
46 }
47 bool check_t(int s[], int k) {
48     for (int i = 0; i < 6; ++i) {
49         if (s[i] != key[i][k]) { return false; }
50     }
51     return true;
52 }
53 bool check(int x, int s[]) {
54     x = head[x];
55     while (x != -1) {
56         if (check_t(s, x)) { return true; } x = Next[x];
57     }
58     return false;
59 }
60 
61 int main() {
62 #ifdef __AiR_H
63     freopen("in.txt", "r", stdin);
64 //    freopen("out.txt", "w", stdout);
65 #endif
66     scanf("%d", &n); bool flag = false; int t1, t2;
67     memset(head, -1, sizeof(head));
68     while (n--) {
69         for (int i = 0; i < 6; ++i) { scanf("%d", &a[i]); a_t[i] = a[i]; }
70         if (flag) { continue; } reverse(a_t, a_t + 6);
71         t1 = cal(a); if (check(t1, a)) { flag = true; continue; }
72         t2 = cal(a_t); if (check(t2, a_t)) { flag = true; continue; }
73         insert(t1, a); insert(t2, a_t);
74     }
75     if (flag) { printf("Twin snowflakes found.\n"); }
76     else { printf("No two snowflakes are alike.\n"); }
77     return 0;
78 }
View Code