题意:给定一篇文章,将每个单词的首尾字母不变,中间顺序打乱,然后将单词之间的空格去掉,得到一个序列,给出一个这样的序列,给你一个字典,将原文翻译出来。

析:在比赛的时候读错题了,忘记首尾字母不变了,一直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;
}

HDU 2340 Obfuscation (暴力)的更多相关文章

  1. HDU 2340 Obfuscation(dp)

    题意:已知原串(长度为1~1000),它由多个单词组成,每个单词除了首尾字母,其余字母为乱序,且句子中无空格.给定n个互不相同的单词(1 <= n <= 10000),问是否能用这n个单词 ...

  2. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  3. HDU 5522 Numbers 暴力

    Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...

  4. hdu 5077 NAND(暴力打表)

    题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后依据x1,x2,x3的值构造出8个字符.如今给定要求生成的8个字符.问 说Xiaoqiang最少要写多少行代码. ...

  5. hdu 5726 GCD 暴力倍增rmq

    GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...

  6. hdu 4291(矩阵+暴力求循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...

  7. HDU 4462(暴力枚举)

    因为题目当中的k比较小k <= 10,所以可以直接枚举,题目里面由两个trick, 一个是如果每个点都可以放稻草人的话,那么答案是0, 另外一个就是如果可以放稻草人的点不用被照到.知道了这两个基 ...

  8. hdu 4499 Cannon(暴力)

    题目链接:hdu 4499 Cannon 题目大意:给出一个n*m的棋盘,上面已经存在了k个棋子,给出棋子的位置,然后求能够在这种棋盘上放多少个炮,要求后放置上去的炮相互之间不能攻击. 解题思路:枚举 ...

  9. hdu 4277 USACO ORZ (暴力+set容器判重)

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. 【BZOJ3626】LCA(树上差分,树链剖分)

    题意:给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q次询问,每次询问给 ...

  2. BZOJ2196: [Usaco2011 Mar]Brownie Slicing

    n<=500 * m<=500的方阵,先沿横坐标切A-1刀,再把每一块切B-1刀,得到A*B块,求这A*B块的数字之和的最小值的最大值. 最小值最大--二分,然后贪心切.每次扫一行,看这一 ...

  3. C++内存分配方式(——选自:C++内存管理技术内幕)

    C++内存分配的区: 1.栈:程序运行时分配的,局部变量,以及传入的参数等存储的地方,在程序结束的时候会回收 2.堆:new分配,由delete释放 3.自由存储区:malloc分配 4.全局/静态存 ...

  4. SeaJS项目完整实例【转】

    index.html——主页面. sea.js——SeaJS脚本. init.js——init模块,入口模块,依赖data.jquery.style三个模块.由主页面载入. data.js——data ...

  5. Tell me the area---hdu1798 (数学 几何)

    http://acm.hdu.edu.cn/showproblem.php?pid=1798 给你两个圆求阴影部分的面积 求出两个扇形的面积减去四边形的面积 扇形的面积是度数(弧度制)*半径的平方 不 ...

  6. 2017CodeM初赛B场

    A.合并字符串价值(loj6174) 分析: 普通暴力:枚举两个分界线,那么ans=Σmin(Al(c)+Bl(c),Ar(c)+Br(c)),这样是O(n^2),会TLE 考虑枚举a的分界线,b的答 ...

  7. css三大特性

    层叠性: 当多个样式(样式的优先级相同)作用于同一个(同一类)标签时,样式发生了冲突,总是执行后边的代码(后边代码层叠前边的代码).和标签调用选择器的顺序没有关系. 继承性: 文字的大多属性都可以继承 ...

  8. 2018 11.1 PION 模拟赛

    期望:250  100+100+50 实际:210   80+100+30 期望:100   实际:80 最后:两个点T了.可能是求逆元的方法太慢了,也可能是闲的又加了一个快速乘的原因. #inclu ...

  9. Java中的重写

    以下内容引用自http://wiki.jikexueyuan.com/project/java/overriding.html: 如果一个类从它的父类继承了一个方法,如果这个方法没有被标记为final ...

  10. HttpUtils 用于进行网络请求的工具类

    原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...