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 ...
随机推荐
- Snapshot Standby
INTRODUCTION Snapshot standby database是ORACLE 11g的新特性.允许Physical standby短时间的使用read write模式. Snapshot ...
- JavaScript笔记(2)
-->变量的定义 1.取得并使用值是所有程序设计中的要点 2.JS中的变量是没有类型的,在JS中只有值才持有类型,变量所持有的是其对应值的类型. 3.变量的取名要符合标识符的规则: (1)一个J ...
- 交换工资 SQL
- CSS控制显示超出部分,用省略号显示
经常使用.可是常忘,我又不是写css的.所以记下来: 先设置一下限制的宽度, display:block; white-space:nowrap; overflow:hidden; text-over ...
- tcpdump重要笔记
无关痛痒的參数就不写了.仅仅说一些我觉得值得注意的. 1 tcpdump參数 -s 最早在公司旧机器上截包时发现总是不完整,于是知道了这个參数,之后就一直用-s0了.近期一次在家里,忘记输入-s发现包 ...
- Loadrunner经典测试实例
Loadrunner经典测试实例
- CISP/CISA 每日一题 14
CISA 每日一题(答) 自动无人值守运行(LIGHTS-OUT)优势:1.信息系统运行成本的遏制/减少:2.持续运行(24/7):3.减少系统错误和中断次数. I/O 控制人员负责保证:1.批处理信 ...
- [Angular] Custom directive Form validator
Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...
- 用电脑从Google Play下载apk
用电脑从Google Play下载apk 方法一:给Chrome浏览器安装apk-downloader插件,需禁止 SSL 错误警告,即在Chrome的快捷方式上加入"--ignore-ce ...
- actionbar spinner-用法实例
今天需要更改一个actionbar上的spinner的字体颜色,结果试了好长时间都没有解决,最后才发现,原来他是在代码下增加的一个textview,然后使用adapter加载的,并不是直接用frame ...