UVa Problem 850 Crypt Kicker II(復号II)


// Crypt Kicker II (   II)
// PC/UVa IDs: 110304/850, Popularity: A, Success rate: average Level: 2
// Verdict: Accepted
// Submission Date: 2011-05-23
// UVa Run Time: 0.016s
//
//     (C)2011,  。metaphysis # yeah dot net
	
#include <iostream>
#include <vector>
#include <sstream>
#include <iterator>
	
using namespace std;
	
void decipher(vector < string > & plain, vector < vector < string > > & encrypted)
{
	string cipher(26, '*');
	for (vector < vector < string > >::iterator t = encrypted.begin(); t !=
					 encrypted.end(); t++)
		//                     。
		if ((*t).size() == plain.size())
		{
			//                         。
			vector < string >::iterator a = (*t).begin();
			vector < string >::iterator b = plain.begin();
			for (; a != (*t).end() && (*a).length() == (*b).length(); a++, b++)
				;
			//        ,    。
			if (a == (*t).end())
			{
				//       ,         。
				string tmp = cipher;
				bool matched = true;
				for (a = (*t).begin(), b = plain.begin(); a != 
							(*t).end(); a++, b++)
				{
					string e = *a;
					string p = *b;
					
					for (int i = 0; i < e.length(); i++)
						//                  ,
						//              。
						if (cipher[e[i] - 'a'] == '*')
						{
							cipher[e[i] - 'a'] = p[i];
						}
						else
						{
							//              
							//    ,     
							//             ,
							//    ,      。
							if (cipher[e[i] - 'a'] != p[i])
							{
								matched = false;
								break;
							}
						}
						
					if (!matched)
						break;
				}
				
				//       ,   cipher    ,           。
				if (matched)
				{
					for (vector < vector < string > >::iterator t =
					 encrypted.begin(); t != encrypted.end(); t++)
					{
						for (vector < string >::iterator v = 
						(*t).begin(); v != (*t).end(); v++)
							{
								string tmp = (*v);
								for (int j = 0; j < tmp.length(); j++)
									cout << cipher[tmp[j] - 'a'];
								if (v < (*t).end() - 1)
									cout << " ";
							}
							
						cout << endl;
					}
					
					return;
				}
				else
					cipher = tmp;
			}
					
		}
		
		//   。
		cout << "No solution." << endl;
}
	
int main(int ac, char *av[])
{
	vector < vector < string > > encrypted;		//     。
	vector < string > plain;			//     。
	string word, line;				//     ,   。
	int cases;					//       。
	int current = 0;				//       。
	
	//         。
	istringstream iss("the quick brown fox jumps over the lazy dog");
	while (iss >> word)
		plain.push_back(word);
	
	//       。
	cin >> cases;
	cin.ignore();
	
	//     。
	getline(cin, line);
	
	while (current < cases)
	{
		encrypted.clear();
	
		//     ,      。
		while (getline(cin, line), line.length() > 0)
		{
			//                       。
			iss.clear();
			iss.str(line);
			vector < string > tmp;
			while (iss >> word)
				tmp.push_back(word);
				
			encrypted.push_back(tmp);
			
		}
		
		//   。
		decipher(plain, encrypted);
		
		current++;
		
		if (current < cases)
			cout << '
'; } return 0; }