Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1657

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1657

Automatic Correction of Misspellings

 

Local Contest, University of Ulm DE Germany

Timelimit: 1

Some text editors offer a feature to correct words which seem to be written incorrectly. In this problem you are asked to implement a simple Automatic Correction of Misspellings (ACM).

ACM takes care of the following misspellings of words:

  1. One letter is missing (e.g., letter is written leter) or too much (e.g., letter is written lettter).
  2. One letter is wrong (e.g., letter is written ketter).
  3. The order of two adjacent letters is wrong (e.g., letter is written letter).

ACM is based on a dictionary of known words. When a text contains a word which is not in the dictionary, ACM will try to replace it by a similar word of the dictionary. Two words are similar if we can transform one word into the other by doing exactly one of the misspellings listed above. An unknown word is left unchanged if there is no similar word in the dictionary.

 

Input

 

The first line of the input file will give the number N of words in the dictionary (N ≤ 10000). The next N lines contain the dictionary words. The following line contains an integer Q ≤ 1000, the number of query words. The next Q lines contain the query words. You may assume that each word in the input consists of 1 to 25 lower case letters ('a' to 'z').

 

Output

 

For each query word, print one line with the query word followed by one of the following possibilities:

  1. is correct, if the word occurs in the dictionary.
  2. is a misspelling of X, where X is a word of the dictionary similar to the query word, and the query word is not in the dictionary. In the case that there are several possibilities, select the word from the dictionary which appeared earlier in the input.
  3. is unknown, if cases 1 and 2 do not apply.

 

 

 

Sample Input Sample Output

10
this
is
a
dictionary
that
we
will
use
for
us
6
su
as
the
dictonary
us
willl

su is a misspelling of us
as is a misspelling of is
the is unknown
dictonary is a misspelling of dictionary
us is correct
willl is a misspelling of will

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>


using namespace std;
vector < string> v;
int n;

int mindist(string y)
{
	int min = INT_MAX;
	int pos = -1;
	int i, j;
	for (int z = 0; z  <  n; ++z)
	{
		string x = v[z];
		int match = 0;
		if ((int)x.size() - (int)y.size()  < = 0)
		{
			for (i = 0; i < x.size(); ++i)
			{
				for (j = i; j  <  y.size(); ++j)
				{
					if (x[i] == y[j]) break;
				}
				if (i > (int)y.size()) break;
				if (j == (int)y.size()) ++match;
			}
			if (min > match && match  <  x.size() && match < y.size())
			{
				min = match;
				pos = z;
			}
		}
	}
	if (pos == -1) return pos;
	if ((int)v[pos].size() - (int)y.size() > 1) return -1;
	return pos;
}

int main()
{
	string y;
	int q;
	v.reserve(10005);
	while(cin >> n)
	{
		v.clear();
		for (int i = 0 ; i  <  n ; ++i)
		{
			cin >> y;
			v.push_back(y);
		}
		cin >> q;
		
		while (q--)
		{
			cin >> y;
			
			int r = mindist(y);
			
			if (r == -1) cout << y << " is unknown\n";
			else if (v[r] == y) cout << y << " is correct\n";
			else cout << y << " is a misspelling of " << v[r] << '\n';
		}
	}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10
this
is
a
dictionary
that
we
will
use
for
us
6
su
as
the
dictonary
us
willl

Output

x
+
cmd
su is a misspelling of us
as is a misspelling of is
the is unknown
dictonary is a misspelling of dictionary
us is correct
willl is a misspelling of will
Advertisements

Demonstration


Previous
#1652 Beecrowd Online Judge Solution 1652 Deli Deli Solution in C, C++, Java, Js and Python
Next
#1663 Beecrowd Online Judge Solution 1663 Ambiguous Permutations Solution in C, C++, Java, Js and Python