首页 > 代码库 > UVa 1399 Puzzle

UVa 1399 Puzzle

方法:AC自动机

先把禁止的string插入ac自动机中,然后再这个自动机上求最长的合法路径。如果出现环或者最长路径长度为0,则输出“No";否则输出最长路径。

code:

技术分享
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57 
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71 
 72 const int maxnode = 4e5+5, sigma_size = 26;
 73 int n, K;
 74 struct AC
 75 {
 76     int sz, g[maxnode][sigma_size];
 77     int fail[maxnode], last[maxnode];
 78     void init()
 79     {
 80         sz = 1; Reset(g[0], 0);
 81     }
 82     int idx(char c)
 83     {
 84         return c-A;
 85     }
 86     int newnode()
 87     {
 88         last[sz] = 0; Reset(g[sz], 0);
 89         return sz++;
 90     }
 91     void insert(const string &str)
 92     {
 93         int n = str.length(), cur = 0;
 94         rep(i, n)
 95         {
 96             int c = idx(str[i]);
 97             if (!g[cur][c]) g[cur][c] = newnode();
 98             cur = g[cur][c];
 99         }
100         last[cur] = 1;
101     }
102     void get_fail()
103     {
104         queue<int> q;
105         fail[0] = 0;
106         rep(i, sigma_size)
107         {
108             int u = g[0][i];
109             if (u)
110             {
111                 fail[u] = 0;
112                 q.push(u);
113             }
114         }
115         while (!q.empty())
116         {
117             int cur = q.front();  q.pop();
118             rep(i, K)
119             {
120                 int u = g[cur][i];
121                 if (!u)
122                 {
123                     g[cur][i] = g[fail[cur]][i]; continue;
124                 }
125                 q.push(u);  int nxt = fail[cur];
126                 while (nxt && !g[nxt][i]) nxt = fail[nxt];
127                 fail[u] = g[nxt][i];
128                 last[u] |= last[fail[u]];
129             }
130         }
131     }
132 } solver;
133 bitset<maxnode> vis;
134 int dp[maxnode], jump[maxnode];
135 
136 int dfs(int cur)
137 {
138     if (vis[cur]) return -1;
139     int &ans = dp[cur];
140     if (ans == -1)
141     {
142         vis[cur] = true;
143         ans = 0;
144         drep(i, K)
145         {
146             int nxt = solver.g[cur][i];
147             if (!solver.last[nxt])
148             {
149                 int tmp = dfs(nxt);
150                 if (tmp == -1) return -1;
151                 if (tmp+1 > ans)
152                 {
153                     ans = tmp+1;
154                     jump[cur] = i;
155                 }
156             }
157         }
158     }
159     vis[cur] = 0;
160     return ans;
161 }
162 
163 int main()
164 {
165     ios::sync_with_stdio(false);
166     cin.tie(0);
167     int T;  cin >> T;
168     repn(kase, T)
169     {
170         cin >> K >> n;
171         solver.init();
172         Reset(dp, -1);
173         Reset(jump, -1);
174         vis.reset();
175         string line;
176         rep(i, n)
177         {
178             cin >> line;
179             solver.insert(line);
180         }
181         solver.get_fail();
182         int ans = dfs(0);
183         if (ans == -1 || !ans)
184         {
185             cout << "No\n";
186         }
187         else
188         {
189             
190             int cur = 0;
191             for(;;)
192             {
193                 if (jump[cur] == -1) break;
194                 cout << (char)(jump[cur]+A);
195                 cur = solver.g[cur][jump[cur]];
196             }
197             cout << newline;
198         }
199     }
200 }
201 
202 /*
203  
204  3 2 4 AAA AB BA BB 2 4 AAA BBB ABAB BBAA 3 7 AA ABA BAC BB BC CA CC
205 
206 */
View Code

 

UVa 1399 Puzzle