programming-challenges Crypt Kicker (110204) 题解
2024-09-08 06:05:48
我的解答,可是复杂度不是非常惬意,是一个指数级的复杂度.可是測试数据比較弱,还是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;
}
最新文章
- Neutron Kilo-Liberty-Mitaka各版本区别
- Windows 10 Threshold 2 升级记录
- jquery插件之tab标签页或滑动门
- Partran,Nastran和ANSYS的区别
- JS初学之-点击元素,当前的显示样式,其他变灰色
- iOS加入百度地图的几个问题
- 进程通信之一 使用WM_COPYDATA C++及C#实现 z
- JavaScript高级程序设计53.pdf
- input里面的查找标记 ő
- IOS--跳转方式两种
- 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等
- mysql原生语句基础知识
- html表格以pdf格式导出到本地
- Form-encoded method must contain at least one @Field.
- 【转】Webdriver的PageObject改造By 张飞
- 使用zabbix3.0.4的ICMP Ping模版实现对客户端网络状态的监控
- Linux下查/删/替 命令(转)
- Skip level 1 on 1
- “一键制作启动u盘失败”的主要原因是什么?
- linux下jdk简单配置记录
热门文章
- 65.Express---express-session
- wpf app全局变量传参方法(代码片段 )
- 通过no-gui模式运行jmeter脚本与生成报告
- Scala——构造函数
- CISP/CISA 每日一题 21
- 洛谷——P1101 单词方阵
- 洛谷 P2118 比例简化
- 洛谷 P1657 选书
- usr/bin/mysqladmin: refresh failed; error: &;#39;Unknown error&;#39;
- OC中对于属性的总结(@property)