我的解答,可是复杂度不是非常惬意,是一个指数级的复杂度.可是測试数据比較弱,还是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;
}

最新文章

  1. Neutron Kilo-Liberty-Mitaka各版本区别
  2. Windows 10 Threshold 2 升级记录
  3. jquery插件之tab标签页或滑动门
  4. Partran,Nastran和ANSYS的区别
  5. JS初学之-点击元素,当前的显示样式,其他变灰色
  6. iOS加入百度地图的几个问题
  7. 进程通信之一 使用WM_COPYDATA C++及C#实现 z
  8. JavaScript高级程序设计53.pdf
  9. input里面的查找标记 ő
  10. IOS--跳转方式两种
  11. 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等
  12. mysql原生语句基础知识
  13. html表格以pdf格式导出到本地
  14. Form-encoded method must contain at least one @Field.
  15. 【转】Webdriver的PageObject改造By 张飞
  16. 使用zabbix3.0.4的ICMP Ping模版实现对客户端网络状态的监控
  17. Linux下查/删/替 命令(转)
  18. Skip level 1 on 1
  19. “一键制作启动u盘失败”的主要原因是什么?
  20. linux下jdk简单配置记录

热门文章

  1. 65.Express---express-session
  2. wpf app全局变量传参方法(代码片段 )
  3. 通过no-gui模式运行jmeter脚本与生成报告
  4. Scala——构造函数
  5. CISP/CISA 每日一题 21
  6. 洛谷——P1101 单词方阵
  7. 洛谷 P2118 比例简化
  8. 洛谷 P1657 选书
  9. usr/bin/mysqladmin: refresh failed; error: &amp;#39;Unknown error&amp;#39;
  10. OC中对于属性的总结(@property)