题意:给定一篇文章,将每个单词的首尾字母不变,中间顺序打乱,然后将单词之间的空格去掉,得到一个序列,给出一个这样的序列,给你一个字典,将原文翻译出来。
析:在比赛的时候读错题了,忘记首尾字母不变了,一直WA。暴力求解,去深搜每个单词,做一些恰当的优化,能不进行的就不进行。胡搞的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <unordered_map> //#include <tr1/unordered_map> #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; //using namespace std :: tr1; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const LL LNF = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e4 + 5; const LL mod = 10000000000007; const int N = 1e6 + 5; const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2}; const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1}; const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); } int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } inline LL Min(LL a, LL b){ return a < b ? a : b; } inline LL Max(LL a, LL b){ return a > b ? a : b; } inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Node{ char s[105]; int id, n; }; struct node{ string s; char pre, last; node(string ss, char p, char l) : s(ss), pre(p), last(l) { } }; unordered_map<string, vector<Node> > mp; bool vis[105]; vector<int> v; vector<node> ans; int cnt; string str; int solve(const string &s, const string &ss){ int ans = 0; for(int i = 0; i < mp[s].size(); ++i) if(mp[s][i].s[0] == ss[0] && mp[s][i].s[mp[s][i].n-1] == ss[ss.size()-1]) ++ans; return ans; } bool dfs(int cur){ if(cur >= str.size()){ ++cnt; return true; } if(cnt > 1) return true; bool ok = false; for(int i = 0; i < v.size() && v[i]+cur <= str.size(); ++i){ string s = str.substr(cur, v[i]); if(s.size() > 2){ string ss = s.substr(1, s.size()-2); sort(ss.begin(), ss.end()); if(!mp.count(ss)) continue; int t = solve(ss, s); if(!t) continue; if(dfs(cur+v[i])){ if(t == 1){ ok = true; ans.push_back(node(ss, s[0], s[s.size()-1])); } else { cnt = 5; return true; } } } else{ if(!mp.count(s)) continue; int t = 0; for(int j = 0; j < mp[s].size(); ++j) if(mp[s][j].n == s.size()) ++t; if(!t) continue; if(dfs(v[i]+cur)){ if(t == 1){ ok = true; ans.push_back(node(s, 0, s.size())); } else { cnt = 5; return true; } } } } return ok; } int main(){ int T; cin >> T; while(T--){ cin >> str; scanf("%d", &m); mp.clear(); ans.clear(); v.clear(); memset(vis, false, sizeof vis); char t[105]; Node u; for(int i = 0; i < m; ++i){ scanf("%s", u.s); int n = strlen(u.s); u.n = n; if(n > 2){ memcpy(t, u.s+1, n-2); t[n-2] = 0; sort(t, t+n-2); u.id = mp[t].size(); mp[t].push_back(u); } else{ memcpy(t, u.s, n+1); u.id = 0; mp[t].push_back(u); } vis[n] = true; } for(int i = 1; i < 105; ++i) if(vis[i]) v.push_back(i); cnt = 0; dfs(0); if(cnt > 1) puts("ambiguous"); else if(!cnt) puts("impossible"); else { for(int i = ans.size()-1; i >= 0; --i){ if(i != ans.size()-1) putchar(' '); node &anss = ans[i]; for(int j = 0; j < mp[anss.s].size(); ++j){ if(anss.pre == 0 && mp[anss.s][j].n == anss.last){ printf("%s", mp[anss.s][j].s); break; } else if(anss.pre == mp[anss.s][j].s[0] && anss.last == mp[anss.s][j].s[mp[anss.s][j].n-1]){ printf("%s", mp[anss.s][j].s); break; } } } printf("\n"); } } return 0; }