您的位置:首页 > 其它

programming-challenges Crypt Kicker II (110304) 题解

2015-06-22 12:02 483 查看
注意错误是输出是:"No solution.",后面有point的

#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> parseString(string &s) {
vector<string> subStrs;
string ts;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
ts.push_back(s[i]);
}
else {
if (!ts.empty()) subStrs.push_back(ts);
ts.clear();
}
}
if (!ts.empty()) subStrs.push_back(ts);
return subStrs;
}

int main() {
string orig = "the quick brown fox jumps over the lazy dog";
vector<string> origSubStrs = parseString(orig);

int TC = 0; cin >> TC;
cin.get(); cin.get();
bool blank = false;
for (int tc = 1; tc <= TC; tc++) {
string s;
vector<string> lines;
if (tc > 1) cin.get();
while (true) {
if (cin.peek() == '\n' || cin.eof()) break;
getline(cin, s);
lines.push_back(s);
}

if (blank) {
cout << endl;
}
blank = true;

bool ok = false;

for (int i = 0; i < lines.size(); i++) {
int sourceToObj[26], objToSource[26];
for (int j = 0; j < 26; j++) {
sourceToObj[j] = objToSource[j] = -1;
}
vector<string> tSubStrs = parseString(lines[i]);
if (tSubStrs.size() != origSubStrs.size()) continue;
ok = true;
for (int j = 0; j < tSubStrs.size() && ok; j++) {
if (tSubStrs[j].size() != origSubStrs[j].size()) {
ok = false;
}
else {
for (int k = 0; k < tSubStrs[j].size() && ok; k++) {
int i1 = tSubStrs[j][k] - 'a';
int i2 = origSubStrs[j][k] - 'a';
if (sourceToObj[i2] != -1 && sourceToObj[i2] != i1) {
ok = false;
}
else if (objToSource[i1] != -1 && objToSource[i1] != i2) {
ok = false;
}
else {
sourceToObj[i2] = i1;
objToSource[i1] = i2;
}
}
}
}

if (ok) {
// output answer
for (int j = 0; j < lines.size(); j++) {
for (int k = 0; k < lines[j].size(); k++) {
if (lines[j][k] == ' ') cout << ' ';
else cout << (char)('a' + objToSource[lines[j][k] - 'a']);
}
cout << endl;
}
break;
}
}
if (!ok) {
cout << "No solution." << endl;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: