programming-challenges Crypt Kicker II (110304) 题解

注意错误是输出是:"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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值