Solution: UVA(10150), PC(110307) — Doublets

Posted: July 23, 2013 in Moderate Problems
Tags: , , , ,

A doublet is a pair of words that differ in exactly one letter; for example, “booster” and “rooster” or “rooster” and “roaster” or “roaster” and “roasted”.

You are given a dictionary of up to 25,143 lowercase words, not exceeding 16 letters each. You are then given a number of pairs of words. For each pair of words, find the shortest sequence of words that begins with the first word and ends with the second, such that each pair of adjacent words is a doublet. For example, if you were given the input pair “booster” and “roasted”, a possible solution would be (“booster,” “rooster,” “roaster,” “roasted”), provided that these words are all in the dictionary.

Input

The input file contains the dictionary followed by a number of word pairs. The dictionary consists of a number of words, one per line, and is terminated by an empty line. The pairs of input words follow; each pair of words occurs on a line separated by a space.

Output

For each input pair, print a set of lines starting with the first word and ending with the last. Each pair of adjacent lines must be a doublet.

If there are several minimal solutions, any one will do. If there is no solution, print a line: “No solution.” Leave a blank line between cases.

Sample Input

booster
rooster
roaster
coasted
roasted
coastal
postal

booster roasted
coastal postal

Sample Output

booster
rooster
roaster
roasted

No solution.

Explanation:

Simple Breadth First Search suffices here. Generate the adjacency matrix initially or when you need it, both are fine. However, the PC judge doesn’t accept this because there are many ways to create the output. Nevertheless, the UVA judge accepts it.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <time.h>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define E 2.71828182845904523536
#define long long LL
using namespace std;


map<string, vector<string> > AdjMat;
bool areDoublets(string a, string b)
{
	int diff=0;
	for (int i=0; i<a.length(); i++)
		if (a[i] != b[i] && ++diff > 1) return false; 
	

	return true;
	
}
void computeAdjMat(vector<string> entries)
{
	for (int i=0; i<entries.size(); i++)
		for (int j=i+1; j<entries.size(); j++)
			if (areDoublets(entries[i],entries[j]))
			{
				AdjMat[entries[i]].push_back(entries[j]);
				AdjMat[entries[j]].push_back(entries[i]);
			}

		
	
}

struct Node
{
	string word;

	Node * Parent;
};


vector<string> shortestPath(string src, string dest)
{
	map<string, string> ParentOf;
	queue<string> ToVisit;
	ToVisit.push(src);

	ParentOf[src] = "";
	bool found=false;
	while(!ToVisit.empty())
	{
		string v = ToVisit.front();
		ToVisit.pop();
		for (int i=0; i<AdjMat[v].size(); i++)
		{
			if (ParentOf.find(AdjMat[v][i]) == ParentOf.end())
				{
					ToVisit.push(AdjMat[v][i]);
					ParentOf[AdjMat[v][i]] = v;
					if (AdjMat[v][i] == dest)
					{
						found = true;
						break;
					}
				}
		}

		if (found) break;
	}

	vector<string> result;

	if (!found)
	{
		result.push_back("No solution.");
		return result;
	}

	string next = dest;
	do
	{
		result.push_back(next);
		next = ParentOf[next];
	}while(result.back() != src);


	return result;
}
int main()
{
	bool CmptedAdjMat[50] = {false};
	char Input[50];
	vector<string> Dictionary[50];
	int counter=0;
	while(1)
	{
		gets(Input);

		if (strcmp(Input,"") == 0) break;
		Dictionary[strlen(Input)].push_back(Input);

	}

	while(gets(Input) != NULL)
	{
		counter++;
		if (counter > 1) cout << endl;
		string src,dest;
		stringstream s(Input);
		s >> src >> dest;

		if (src.length() != dest.length()) cout << "No solution.\n";
		else
		{
			if (!CmptedAdjMat[src.length()])
			{
				CmptedAdjMat[src.length()] = true;
				computeAdjMat(Dictionary[src.length()]);
			}

			vector<string> results = shortestPath(src,dest);

			for (int i=results.size()-1; i>=0; i--)
			{
				cout << results[i] << endl;
			}

		}

	}
	return 0;
}

Leave a comment