HDU 2340 - Obfuscation(dp)
题意:一个句子中有多个单词,但是目前的单词,除了首末两位,中间的单词字母顺序均被打乱,并且打乱后把单词间的空格删掉变成一个新句子。现在给定这个新句子(长度为1~1000),给定n个单词(1 <= n <= 10000),且每个单词是唯一的,求是否能用这n个单词还原出这个句子的原来的样子,若不能则输出"impossible",若多解则输出"ambiguous",否则输出这个句子。(例:tihssnetnceemkaesprfecetsesne,给定makes,perfect,sense,sentence,this,只有一种解读方式:this sentence makes perfect sense)
1、d[i]表示从开始到第 i 位这一段有几种构成方法;
2、从前往后找,看看对于某一位能不能往前延伸构成一个单词,设 i ~ j 位可以构成一个单词,则 d[j] += d[i - 1],最后看d[len - 1]的情况即可;
3、对于每个单词记录前缀和后缀,以及,记录每个单词的字母个数和目标串截止到每一位的字母个数,方便减小枚举量;
4、边dp边记录达到状态转移的要求的某一位的来源。
总共两个AC代码,第一次900ms多差点超时,换种写法写之后只有62ms,还是能用char数组就用char数组表示字符串,string太浪费时间。
代码如下:
62ms
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> #define fin freopen("in.txt", "r", stdin) #define fout freopen("out.txt", "w", stdout) #define pr(x) cout << #x << " : " << x << " " #define prln(x) cout << #x << " : " << x << endl #define Min(a, b) a < b ? a : b #define Max(a, b) a < b ? b : a typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const ll MOD = 1e9 + 7; using namespace std;#define NDEBUG #include<cassert> const int MAXN = 1000 + 10; const int MAXT = 10000 + 10;char s[MAXT][110];struct Node{int id, len, num[30];char head;Node(int ids, int le) : id(ids), len(le){memset(num, 0, sizeof num);for(int i = 0; i < len; ++i)++num[s[id][i] - 'a'];head = s[id][0];} };int T, n, plen, d[MAXN], last[MAXN], thisidx[MAXN], thisidy[MAXN], o[MAXN][30]; char p[MAXN]; vector<Node> vec[30];inline bool judge(Node &u, int l, int r){if(l == 0){for(int i = 0; i < 26; ++i)if(u.num[i] != o[r][i]) return false;return true;}else{for(int i = 0; i < 26; ++i)if(u.num[i] != o[r][i] - o[l - 1][i]) return false;return true;} }void init(){for(int i = 0; i < 26; ++i) vec[i].clear();memset(d, 0, sizeof d);memset(thisidy, 0, sizeof thisidx);memset(thisidx, 0, sizeof thisidy);memset(last, -1, sizeof last);memset(o[0], 0, sizeof o[0]);++o[0][p[0] - 'a'];for(int i = 1; i < plen; ++i){for(int j = 0; j < 26; ++j) o[i][j] = o[i - 1][j];++o[i][p[i] - 'a'];} }int main(){scanf("%d", &T);while(T--){scanf("%s", p);plen = strlen(p);init();scanf("%d", &n);for(int i = 0; i < n; ++i){scanf("%s", s[i]);int le = strlen(s[i]);vec[s[i][le - 1] - 'a'].push_back(Node(i, le));}for(int i = 0; i < plen; ++i){int lur = p[i] - 'a';for(int j = 0; j < vec[lur].size(); ++j){Node &u = vec[lur][j];int tmp = i - u.len + 1;if(tmp == 0 && u.head == p[tmp] && judge(u, tmp, i)){++d[i];thisidx[i] = lur;thisidy[i] = j;}else if(tmp > 0 && u.head == p[tmp] && judge(u, tmp, i) && d[tmp - 1]){d[i] += d[tmp - 1];last[i] = tmp - 1;thisidx[i] = lur;thisidy[i] = j;}}}if(!d[plen - 1]) printf("impossible\n");else if(d[plen - 1] > 1) printf("ambiguous\n");else{bool flag = false;stack<pair<int, int> > st;for(int i = plen - 1; i != -1; i = last[i])st.push(pair<int, int>(thisidx[i], thisidy[i]));while(!st.empty()){if(flag) printf(" ");pair<int, int> tmp = st.top(); st.pop();printf("%s", s[vec[tmp.first][tmp.second].id]);flag = true;}printf("\n");}}return 0; }
967ms
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> #define fin freopen("in.txt", "r", stdin) #define fout freopen("out.txt", "w", stdout) #define pr(x) cout << #x << " : " << x << " " #define prln(x) cout << #x << " : " << x << endl #define Min(a, b) a < b ? a : b #define Max(a, b) a < b ? b : a typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const ll MOD = 1e9 + 7; using namespace std;#define NDEBUG #include<cassert> const int MAXN = 1000 + 10; const int MAXT = 10000 + 10;struct Node{string aftersort;int id, len;char head;Node(string &s, int ids){id = ids;len = s.length();aftersort = s;if(len > 3) sort(aftersort.begin() + 1, aftersort.end() - 1);head = aftersort[0];} };int T, n, plen, d[MAXN], last[MAXN], thisidx[MAXN], thisidy[MAXN]; string s[MAXT], p; vector<Node> vec[30];inline bool judge(Node &u, int i){string tmp = p.substr(i, u.len);if(u.len >= 3) sort(tmp.begin() + 1, tmp.end() - 1);return tmp == u.aftersort; }void init(){for(int i = 0; i < 26; ++i) vec[i].clear();memset(d, 0, sizeof d);memset(thisidy, 0, sizeof thisidx);memset(thisidx, 0, sizeof thisidy);memset(last, -1, sizeof last); }int main(){scanf("%d", &T);while(T--){init();cin >> p;plen = p.length();scanf("%d", &n);for(int i = 0; i < n; ++i){cin >> s[i];vec[*s[i].rbegin() - 'a'].push_back(Node(s[i], i));}for(int i = 0; i < plen; ++i){int lur = p[i] - 'a';for(int j = 0; j < vec[lur].size(); ++j){Node &u = vec[lur][j];int tmp = i - u.len + 1;if(tmp == 0 && u.head == p[tmp] && judge(u, tmp)){++d[i];thisidx[i] = lur;thisidy[i] = j;}else if(tmp > 0 && u.head == p[tmp] && judge(u, tmp) && d[tmp - 1]){d[i] += d[tmp - 1];last[i] = tmp - 1;thisidx[i] = lur;thisidy[i] = j;}}}if(!d[plen - 1]) printf("impossible\n");else if(d[plen - 1] > 1) printf("ambiguous\n");else{bool flag = false;stack<pair<int, int> > st;for(int i = plen - 1; i != -1; i = last[i])st.push(pair<int, int>(thisidx[i], thisidy[i]));while(!st.empty()){if(flag) printf(" ");pair<int, int> tmp = st.top(); st.pop();printf("%s", s[vec[tmp.first][tmp.second].id].c_str());flag = true;}printf("\n");}}return 0; }
转载于:https://www.cnblogs.com/tyty-TianTengtt/p/6013485.html
HDU 2340 - Obfuscation(dp)相关推荐
- HDU 2340 Obfuscation(dp)
题意:已知原串(长度为1~1000),它由多个单词组成,每个单词除了首尾字母,其余字母为乱序,且句子中无空格.给定n个互不相同的单词(1 <= n <= 10000),问是否能用这n个单词 ...
- hdu 4433 locker(DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4433 题目大意:就是给你一个序列,相当于一个可以转的那种密码锁的初始状态,0往下转是9,9往上转是0,现在 ...
- HDU 2059 龟兔赛跑(DP)
题意 中文 简单的多阶段决策DP 令p[0]=0 p[n]=l d[i]表示乌龟从起点到第i个加油站所需的最小时间 那么有d[i]=min(d[i],d[j]+t(j,i)) ...
- hdu 2154 跳舞毯 (DP)
点击打开链接 dp[i]=dp[i-1]+2*dp[i-2] #include"stdio.h" int main() {__int64 dp[1005];int i;int n; ...
- HDU 1248 冰封王座(dp)
Problem Description 不死巫妖王拉工资,死亡骑士得到N美元的钞票(记,只有一个纸币),战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前. 死亡骑士:"我要买 ...
- HDU 2859 Phalanx(DP)
基础DP #include<iostream> #include<algorithm> #include<cstring> #include<string&g ...
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- HDU-1284:钱币兑换问题 推理+动态规划(dp)
文章目录 题目大意: 题目链接HDU 1284(点击可进入网页提交) 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. 输入: 每行只有一个正整数N, ...
- 求三角形最大面积(DP)
求三角形最大面积(DP) 在OJ上奇迹般WA了:WA:70. Why? #include <iostream> #include <string.h> using namesp ...
最新文章
- ASP.NET MVC 的 WebGrid 的 6 个重要技巧 【已翻译100%】
- 探索Android中的Parcel机制(上) .
- php中插入表格 标签,PHP_HTML中的表格元素,一,table标签。tablegt - phpStudy
- 蔡成功贷款警示:没事别玩高利贷
- 通用的权限管理系统发布
- (47)FPGA同步复位与异步复位(异步复位同步释放)
- java list 遍历查找_Java用list储存,遍历,查询指定信息过程详解
- 中国物联网激荡 20 年
- 【数据结构amp;amp;等差数列】KMP简介和算法的实现(c++ amp;amp; java)
- 简单方法解决bootstrap3 modal异步加载只一次的问题
- java使用libreoffice_关于java使用libreoffice以及openoffice转换问题。
- Java I/O(输入输出流)
- Vue入门之常用指令
- java单元测试(@Test)
- Chisel Bootcamp安装说明
- 九九乘法表打印Python
- K.dot和K.batch_dot
- 认识kata-containers
- 了解软件工程与计算机科学的联系与区别
- JavaScript、Lua语言基础、电脑脚本、手机免ROOT免越狱脚本开发免费视频教程