我的解答,可是复杂度不是非常惬意,是一个指数级的复杂度.可是測试数据比較弱,还是ac了。在网上找了找。都是brute force的解法,不知道有没有更好的解法。

解答中犯了两个错误,第一个。map<int, vector<int>> 的定义不被接受。可是这肯定是一个合法的c++定义。第二个,忘了考虑映射字符间反向的约束。也就是"ab"可能会被翻译成"cc"。这是错误的。字符间从源到目标,从目标到源。都应该不存在一对多的映射。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h> using namespace std; vector<string> words;
vector<string> m_dic[81];
vector<int> finalMatchRelation; bool findMatchString(int index, vector<int> matchedCharacter, vector<int> getMatched) {
if (index >= words.size()) {
finalMatchRelation = matchedCharacter;
return true;
} vector<int> matchedCharacterBackup = matchedCharacter;
vector<int> getMatchedBackup = getMatched; int l = words[index].size();
for (int i = 0; i < m_dic[l].size(); i++) {
bool ok = true;
for (int j = 0; j < words[index].size() && ok; j++) {
int srcCharIndex = words[index][j] - 'a';
int objCharIndex = m_dic[l][i][j] - 'a'; if (matchedCharacter[srcCharIndex] == -1 && getMatched[objCharIndex] == -1) {
matchedCharacter[srcCharIndex] = objCharIndex;
getMatched[objCharIndex] = srcCharIndex;
}
else if (matchedCharacter[srcCharIndex] == (m_dic[l][i][j] - 'a')) {
continue;
}
else {
ok = false;
}
} if (ok) {
bool goodResult = findMatchString(index + 1, matchedCharacter, getMatched);
if (goodResult) {
return true;
}
} matchedCharacter = matchedCharacterBackup;
getMatched = getMatchedBackup;
}
return false;
} int main() {
string placeHolder;
int n;
cin >> n;
getline(cin, placeHolder); for (int i = 0; i < n; i++) {
string ts;
getline(cin, ts);
m_dic[ts.size()].push_back(ts);
} string s;
while (getline(cin, s)) {
vector<int> matchRelation(26, -1), getMatched(26, -1); stringstream ss(s);
string ts;
words.clear();
while (ss >> ts) {
words.push_back(ts);
} string result = "";
if (findMatchString(0, matchRelation, getMatched)) {
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ')
result.push_back(' ');
else
result.push_back('a' + finalMatchRelation[s[i] - 'a']);
}
}
else {
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ')
result.push_back(' ');
else
result.push_back('*');
}
}
cout << result << endl;
} return 0;
}

programming-challenges Crypt Kicker (110204) 题解的更多相关文章

  1. 2017-2018 ACM-ICPC Latin American Regional Programming Contest J - Jumping frog 题解(gcd)

    题目链接 题目大意 一只青蛙在长度为N的字符串上跳跃,"R"可以跳上去,"P"不可以跳上去. 字符串是环形的,N-1和0相连. 青蛙的跳跃距离K的取值范围是[1 ...

  2. HHKB Programming Contest 2020 D - Squares 题解(思维)

    题目链接 题目大意 给你一个边长为n的正方形和边长为a和b的正方形,要求把边长为a和b的正方形放在长度为n的正方形内,且没有覆盖(可以相邻)求有多少种放法(mod 1e9+7) 题目思路 这个思路不是 ...

  3. Social Infrastructure Information Systems Division, Hitachi Programming Contest 2020 D题题解

    将题意转换为一开始\(t = 0\),第\(i\)个操作是令\(t \leftarrow (a_i + 1) t + (a_i + b_i + 1)\).记\(A_i = a_i + 1, B_i = ...

  4. Social Infrastructure Information Systems Division, Hitachi Programming Contest 2020 C题题解

    首先,我们将题目理解成若\(i\)与\(j\)距离恰好为\(3\),则不可能\(p_i \equiv p_j \equiv 1 \space or \space 2 (\bmod 3)\).这就相当于 ...

  5. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  6. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  7. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  8. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  9. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

随机推荐

  1. Repeater控件的

    http://blog.csdn.net/zhang_xinxiu/article/details/21872433 想起来,公司的aspx页面前台数据展示除了datagrid以为还有Repeater ...

  2. the night the room

    http://bogifabian.com/?page_id=2529 I am trying to creat dreamful atmospheres, paint walls and floor ...

  3. C/C++(基础编码-补码详解)

    两个数的交换 1.引入第三者. 2.求和运算,求差.(这样会产生内存溢出) 3.异或运算 a = a^b; b = a^b; a = a^b; 8b(bit位) = 1B(Byte=字节)//最小单位 ...

  4. 【Django】实现跨域请求

    目录 JsonP实现跨域 在Django中间件中添加响应头 @ *** CORS 即 Cross Origin Resource Sharing 跨域资源共享. 跨域请求分两种:简单请求.复杂请求. ...

  5. C# WinForm设置透明

    1:通过设置窗体的 TransparencyKey实现  例:窗体中的白色会变成透明      this.BackColor =Color.White; this.TransparencyKey = ...

  6. [置顶] Docker学习总结(5)——超实用Docker入门学习教程

    Docker是什么 Docker是一种容器技术,它可以将应用和环境等进行打包,形成一个独立的,类似于iOS的APP形式的"应用",这个应用可以直接被分发到任意一个支持Docker的 ...

  7. POJ——T 3250 Bad Hair Day

    http://poj.org/problem?id=3250 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19619   ...

  8. Android 阅读器架构图,网上收集,留做存货

    这个结构图是网上收集的图片.基结构明晰简洁.易于后期维护.本文会继续收集很多其他其他优秀的结构图,望有图的朋友推荐~

  9. ubuntu-date命令的使用

    date命令是关于时间的命令.它可以用来查看.更改系统时间 它的基本格式为 date "+ %H" 注意 "+"是不可以省略的.结果如下 zhangshuli@ ...

  10. 10lession-if-else条件语句

    python的条件选择语句跟其他语言的及其相似,这里就不做详细记录,仅仅是看个例子好了 #!/usr/bin/python "]: print('1 in [1,2,3,"4&qu ...