programming-challenges Crypt Kicker (110204) 题解
我的解答,可是复杂度不是非常惬意,是一个指数级的复杂度.可是測试数据比較弱,还是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) 题解的更多相关文章
- 2017-2018 ACM-ICPC Latin American Regional Programming Contest J - Jumping frog 题解(gcd)
题目链接 题目大意 一只青蛙在长度为N的字符串上跳跃,"R"可以跳上去,"P"不可以跳上去. 字符串是环形的,N-1和0相连. 青蛙的跳跃距离K的取值范围是[1 ...
- HHKB Programming Contest 2020 D - Squares 题解(思维)
题目链接 题目大意 给你一个边长为n的正方形和边长为a和b的正方形,要求把边长为a和b的正方形放在长度为n的正方形内,且没有覆盖(可以相邻)求有多少种放法(mod 1e9+7) 题目思路 这个思路不是 ...
- 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 = ...
- 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)\).这就相当于 ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
随机推荐
- ArraySegment 的使用 【转载】
本文转载自 http://www.cnblogs.com/overred/archive/2009/01/17/1359126.html ArraySegment顾名思义就是Array区块,数组的小抽 ...
- js中的组合模式
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Laravel输出sql语句
$queries = DB::getQueryLog();
- echo---打印变量或输出字符串
cho命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下e ...
- 一个简单RPC框架是怎样炼成的(II)——制定RPC消息
开局篇我们说了,RPC框架的四个核心内容 RPC数据的传输. RPC消息 协议 RPC服务注冊 RPC消息处理 以下,我们先看一个普通的过程调用 class Client(object): def _ ...
- SSH密码错误几次后封禁登录IP
#!/bin/bash yum -y install vixie-cron crontabs mkdir -p /usr/local/cron/ cat > /usr/local/cron/ss ...
- js19--继承终极版本
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 在Qt Creator的项目中添加头文件和库
在Qt Creator中的工程中,工程通过.pro文件管理. 额外需要连接的连接库unix:LIBS += -L your_lib_path -lyour_libwin32:LIBS += your_ ...
- 用VXE保护Linux系统安全
本文被转载在:http://www.linuxso.com/a/linuxxitongguanli/161.html 650) this.width=650;" onclick=&quo ...
- Spring源码分析专题 —— 阅读指引
阅读源码的意义 更深入理解框架原理,印象更深刻 学习优秀的编程风格.编程技巧.设计思想 解决实际问题,如修复框架中的bug,或是参考框架源码,结合实际业务需求编写一个独有的框架 阅读源码的方法 首先是 ...