Obfuscation_HDU - 2340
来源:互联网 发布:淘宝最新市值 编辑:程序博客网 时间:2024/06/07 06:05
题意分析:
题目中的加密字符串有其固定的组成规则:首尾字母不变,中间的字母顺序被打乱,然后去掉空格组成。所以,解译这段字符串的思路就是:匹配首字母相同的, 再去匹配尾字母相同的,最后再去比较中间字符是否相同。
其中需要注意两点:
第一点:当对首尾字母之间的字母排序时, 特别注意当 len < 4 的时候, 否则会WA。
第二点:当在中间某位置又多种选择可以达到时, 要将其标记下来,并将此状态维护到该种情况下所能达到的最后步,最后只需查看最后一步的状态即可。
Description
It is a well-known fact that if you mix up the letters of a word, while leaving the first and last letters in their places, words still remain readable. For example, the sentence “tihs snetncee mkaes prfecet sesne”, makes perfect sense to most people.
If you remove all spaces from a sentence, it still remains perfectly readable, see for example: “thissentencemakesperfectsense”, however if you combine these two things, first shuffling, then removing spaces, things get hard. The following sentence is harder to decipher: “tihssnetnceemkaesprfecetsesne”.
You’re given a sentence in the last form, together with a dictionary of valid words and are asked to decipher the text.
If you remove all spaces from a sentence, it still remains perfectly readable, see for example: “thissentencemakesperfectsense”, however if you combine these two things, first shuffling, then removing spaces, things get hard. The following sentence is harder to decipher: “tihssnetnceemkaesprfecetsesne”.
You’re given a sentence in the last form, together with a dictionary of valid words and are asked to decipher the text.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
One line with a string s: the sentence to decipher. The sentence consists of lowercase letters and has a length of at least 1 and at most 1 000 characters.
One line with an integer n with 1 ≤ n ≤ 10 000: the number of words in the dictionary.
n lines with one word each. A word consists of lowercase letters and has a length of at least 1 and at most 100 characters. All the words are unique.
One line with a string s: the sentence to decipher. The sentence consists of lowercase letters and has a length of at least 1 and at most 1 000 characters.
One line with an integer n with 1 ≤ n ≤ 10 000: the number of words in the dictionary.
n lines with one word each. A word consists of lowercase letters and has a length of at least 1 and at most 100 characters. All the words are unique.
Output
Per testcase:
One line with the deciphered sentence, if it is possible to uniquely decipher it. Otherwise “impossible” or “ambiguous”, depending on which is the case.
One line with the deciphered sentence, if it is possible to uniquely decipher it. Otherwise “impossible” or “ambiguous”, depending on which is the case.
Sample Input
3tihssnetnceemkaesprfecetsesne
5
makes
perfect
sense
sentence
this
hitehre
2
there
hello
hitehre
3
hi
there
three
Sample Output
this sentence makes perfect sense
impossible
ambiguous
ps:这一份是用bfs写的, 也可以用dfs写(虽然我的一直T,没改出来T...T),,,
上代码:
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <map>using namespace std;const int maxn = 10000 + 10;int n, len, ok, repeat, vis[maxn];string text;char dic[maxn][120];struct node { int len, id; char rear; string sorts;};vector <node> words[30];void bfs() { vector <int> ans[1005]; memset(vis, 0, sizeof vis); vis[0] = 1; queue <int> q; q.push(0); while (q.size()) { int pos = q.front(); q.pop(); if (pos >= len) continue; int t = text[pos] - 'a'; for (int i = 0; i < words[t].size(); i++) { node u = words[t][i]; int next_pos = pos + u.len; if (next_pos > len || text[next_pos - 1] != u.rear) continue; string temp = text.substr(pos, u.len); if (temp.length() > 3) sort(temp.begin() + 1, temp.end() - 1); if (temp == u.sorts) { if (!vis[next_pos]) { if (vis[pos] == 1) { ans[next_pos] = ans[pos]; ans[next_pos].push_back(u.id); } else vis[next_pos] += 2; q.push(next_pos); } vis[next_pos] ++; } } } if (!vis[len]) printf("impossible\n"); else if (vis[len] > 1) printf("ambiguous\n"); else { for (int i = 0; i < ans[len].size(); i++) { printf("%s", dic[ans[len][i]]); if (i < ans[len].size() - 1) printf(" "); else printf("\n"); } }}int main() { int T; scanf("%d", &T); getchar(); while (T--) { scanf("%s", dic[0]); text = dic[0]; len = text.length(); for (int i = 0; i < 30; i++) words[i].clear(); scanf("%d", &n); getchar(); node temp; for (int i = 1; i <= n; i++) { scanf("%s", dic[i]); temp.sorts = dic[i]; temp.len = temp.sorts.length(); temp.rear = temp.sorts[temp.len - 1]; temp.id = i; if (temp.len > 3) sort(temp.sorts.begin() + 1, temp.sorts.end() - 1); words[temp.sorts[0] - 'a'].push_back(temp); } bfs(); } return 0;}
0 0
- Obfuscation_HDU - 2340
- hdu 2340 Obfuscation
- HDU 2340 Obfuscation 乱搞
- hrbust 2340 一品五彩
- zoj 2340 Little Jumper(数学题,三分)
- zoj 2340 Little Jumper(三分 物理好题)
- HDU 2340 Obfuscation (DP--记忆话搜索)
- nyoj 2340 最小循环节(KMP之next数组的应用)
- 4.14-1 图片太大遇到的问题,Bitmap too large to be uploaded into a texture (2340x4160, max=4096x4096)
- codeforces-445【A思维】【B并查集】
- Python使用with时本猿犯了一个错误
- SQLite数据库相关知识
- JSF: Email Address Not Stripping Spaces on Validation
- Python高阶函数--map
- Obfuscation_HDU - 2340
- 转自csdn排名第一博主 成功的背后
- 图解HashMap和HashSet的内部工作机制
- 计算几何
- Spark构建分类模型
- JAVA入门笔记2:选择结构语句
- 20161101 学习心得
- 301. Remove Invalid Parentheses(bfs,hard)
- VR预览