构建trie树,可以得到4类结点:必胜点,必负点,完全主宰点(可胜可负),完全无法主宰点(无法控制最终胜负)。递归到叶子结点,即为必胜点,回溯分情况讨论。注意叶子结点使用属性n来控制,n表示当前结点的儿子结点的数目,叶子结点没有儿子。
1 /* 456D */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 using namespace std; 19 20 typedef struct trie_t { 21 int n; 22 int next[26]; 23 trie_t() { 24 n = 0; 25 memset(next, 0, sizeof(next)); 26 } 27 } trie_t; 28 29 const int maxn = 1e5+5; 30 31 trie_t T[maxn]; 32 int L = 0; 33 char s[maxn]; 34 int n, m; 35 36 int newTrie() { 37 return ++L; 38 } 39 40 void create(char *s, int rt) { 41 int p = rt; 42 int i = 0, id; 43 44 while (s[i]) { 45 id = s[i] - ‘a‘; 46 if (T[p].next[id] == 0) { 47 T[p].next[id] = ++L; 48 ++T[p].n; 49 } 50 p = T[p].next[id]; 51 ++i; 52 } 53 } 54 55 int dfs(int rt) { 56 int i; 57 int st = 0; 58 59 if (T[rt].n == 0) 60 return 2; 61 62 for (i=0; i<26; ++i) { 63 if (T[rt].next[i]) 64 st |= dfs(T[rt].next[i]); 65 } 66 switch(st) { 67 case 0: 68 return 3; 69 case 1: 70 return 2; 71 case 2: 72 return 1; 73 case 3: 74 return 0; 75 default: 76 return 0; 77 } 78 } 79 80 81 int main() { 82 int i, j, k; 83 84 #ifndef ONLINE_JUDGE 85 freopen("data.in", "r", stdin); 86 freopen("data.out", "w", stdout); 87 #endif 88 89 scanf("%d %d", &n, &m); 90 // build trie 91 for (i=0; i<n; ++i) { 92 scanf("%s", s); 93 create(s, 0); 94 } 95 // get the key point 96 int st = 0; 97 for (i=0; i<26; ++i) { 98 if (T[0].next[i]) 99 st |= dfs(T[0].next[i]); 100 } 101 102 if (st == 3) { 103 puts("First"); 104 return 0; 105 } else if (st == 0) { 106 puts("Second"); 107 return 0; 108 } 109 if (st == 1) { 110 puts("Second"); 111 return 0; 112 } 113 if (m & 1) { 114 puts("First"); 115 } else { 116 puts("Second"); 117 } 118 119 #ifndef ONLINE_JUDGE 120 printf("time = %d.\n", (int)clock()); 121 #endif 122 123 return 0; 124 }
时间: 2024-10-10 15:55:53