U E D R , A S I H C RSS

Doublets/문보창

소감

아직 나에게 벅찬 난이도다. 나중에 다시 생각해보자.

코드

~cpp 
#include <iostream>
using namespace std;
#include <cstring>

struct Node
{
	Node * front;
	char * data;
	struct Node * next;
};
	
void inDictionary(Node & dic);
void inWords(Node & word);
void enNode(Node & node, int type = 1);
void isDoublet(Node & dic, Node & word);
void showDoublet(Node * dou);

int main()
{
	Node dictionary;
	Node words;

	inDictionary(dictionary);
	inWords(words);
	isDoublet(dictionary, words);
	return 0;
}

void inDictionary(Node & dic)
{
	enNode(dic);
}

void inWords(Node & word)
{
	enNode(word, 2);	
}

void enNode(Node & node, int type)
{
	char word[17];
	node.front = NULL;
	int count = 0;
	while (cin.peek() != '\n')
	{ 
		if (type == 1)
			cin.getline(word, 17, '\n');
		if (type == 2)
		{
			if (count % 2 == 0)
				cin.getline(word, 17, ' ');
			else 
				cin.getline(word, 17, '\n');
			count++;
		}
		int len = strlen(word);
		
		Node * temp = new Node;
		temp->data = new char[len + 1];
		strcpy(temp->data, word);
		temp->next = NULL;
		
		if (node.front == NULL)
		{
			node.front = temp;
			node.next = node.front;
		}
		else
		{
			node.next->next = temp;
			node.next = node.next->next;
		}
	}
	cin.get();
}

void isDoublet(Node & dic, Node & word)
{
	word.next = word.front;
	char formerWord[17] = "None";
	char first[17], second[17];

	while(word.next != NULL)
	{
		strcpy(first, word.next->data);
		word.next = word.next->next;
		strcpy(second, word.next->data);
		word.next = word.next->next;
		dic.next = dic.front;
		
		Node * doublet = new Node;
		doublet->front = NULL;
		Node * temp = new Node;
		temp->data = new char[strlen(first) + 1];
		strcpy(temp->data, first);
		temp->next = NULL;
		doublet->front = temp;
		doublet->next = doublet->front;
		
		while (true)
		{
			if (dic.next == NULL)
			{
				cout << "No solution.\n";
				break;
			}
			if (strcmp(first, second) == 0)
			{
				showDoublet(doublet);
				break;
			}

			int gap = 0;
			for (int i=0; i<strlen(first); i++)
			{
				if (strlen(first) != strlen(dic.next->data))
				{
					gap--;
					break;
				}
				if (first[i] != dic.next->data[i])
					gap++;
				if (gap > 1)
					break;
			}
			if (gap == 1 && strcmp(formerWord, dic.next->data) != 0)
			{
				Node * temp = new Node;
				temp->data = new char[strlen(first) + 1];
				strcpy(temp->data, dic.next->data);
				temp->next = NULL;
				doublet->next->next = temp;
				doublet->next = doublet->next->next;
				strcpy(formerWord, first);
				strcpy(first, dic.next->data);				
				dic.next = dic.front;
			}			
			else
				dic.next = dic.next->next;
		}
		delete temp, doublet;
		if (word.next != NULL)
			cout << endl;
	}
}

void showDoublet(Node * dou)
{
	dou->next = dou->front;
	while (dou->next != NULL)
	{
		cout << dou->next->data << endl;
		dou->next = dou->next->next;
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:09
Processing time 0.0239 sec