//Don't USE Edit Distance .. It will give you TLE ... :)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
String m = "";
int cases=Integer.parseInt(br.readLine());
for(int i=0;i<cases;i++){
if(i>0)
sb.append("\n");
br.readLine();
HashMap<String,Integer> hm=new HashMap<String,Integer>();
int ind=1;
while(true){
m=br.readLine();
hm.put(m,ind);
ind++;
}
while(true){
m=br.readLine();
if(m.equals("#")){
break;
}
if(hm.containsKey(m)){
sb.append(m).append(" is correct\n");
}else{
sb.append(m).append(":");
HashSet<String> added=new HashSet<String>();
LinkedList<Word> list=new LinkedList<Word>();
//delete one character
for(int j=0;j<m.length();j++){
StringBuilder temp=new StringBuilder();
for(int z=0;z<m.length();z++){
if(z!=j)
temp.append(m.charAt(z));
}
String wanted=temp.toString();
if(!added.contains(wanted)){
if(hm.containsKey(wanted)){
list.add(new Word(wanted, hm.get(wanted)));
added.add(wanted);
}
}
}
//replacing one character
for(int j=0;j<m.length();j++){
StringBuilder temp=new StringBuilder();
for(int z=0;z<m.length();z++){
if(z!=j)
temp.append(m.charAt(z));
}
for (int l = 0; l < 27; l++) {
StringBuilder temp2=new StringBuilder(temp.toString());
temp2.insert(j, (char)('a'+l));
String wanted = temp2.toString();
if (!added.contains(wanted)) {
if (hm.containsKey(wanted)) {
list.add(new Word(wanted, hm.get(wanted)));
added.add(wanted);
}
}
}
}
//inserting one character
for(int j=0;j<m.length()+1;j++){
StringBuilder temp=new StringBuilder(m);
for (int l = 0; l < 27; l++) {
StringBuilder temp2=new StringBuilder(temp.toString());
temp2.insert(j, (char)('a'+l));
String wanted = temp2.toString();
if (!added.contains(wanted)) {
if (hm.containsKey(wanted)) {
list.add(new Word(wanted, hm.get(wanted)));
added.add(wanted);
}
}
}
}
Collections.sort(list);
while(!list.isEmpty()){
sb.append(" ").append(list.remove().getWord());
}
sb.append("\n");
}
}
}
System.out.print(sb);
}
}class Word implements Comparable<Word>{
String word;
int index;
public Word(String word, int index) {
this.word = word;
this.index = index;
}
public int getIndex() {
return index;
}
public String getWord() {
return word;
}
@Override
public int compareTo(Word o) {
if(this.index>o.getIndex()){
return 1;
}
return -1;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
String m = "";
int cases=Integer.parseInt(br.readLine());
for(int i=0;i<cases;i++){
if(i>0)
sb.append("\n");
br.readLine();
HashMap<String,Integer> hm=new HashMap<String,Integer>();
int ind=1;
while(true){
m=br.readLine();
hm.put(m,ind);
ind++;
}
while(true){
m=br.readLine();
if(m.equals("#")){
break;
}
if(hm.containsKey(m)){
sb.append(m).append(" is correct\n");
}else{
sb.append(m).append(":");
HashSet<String> added=new HashSet<String>();
LinkedList<Word> list=new LinkedList<Word>();
//delete one character
for(int j=0;j<m.length();j++){
StringBuilder temp=new StringBuilder();
for(int z=0;z<m.length();z++){
if(z!=j)
temp.append(m.charAt(z));
}
String wanted=temp.toString();
if(!added.contains(wanted)){
if(hm.containsKey(wanted)){
list.add(new Word(wanted, hm.get(wanted)));
added.add(wanted);
}
}
}
//replacing one character
for(int j=0;j<m.length();j++){
StringBuilder temp=new StringBuilder();
for(int z=0;z<m.length();z++){
if(z!=j)
temp.append(m.charAt(z));
}
for (int l = 0; l < 27; l++) {
StringBuilder temp2=new StringBuilder(temp.toString());
temp2.insert(j, (char)('a'+l));
String wanted = temp2.toString();
if (!added.contains(wanted)) {
if (hm.containsKey(wanted)) {
list.add(new Word(wanted, hm.get(wanted)));
added.add(wanted);
}
}
}
}
//inserting one character
for(int j=0;j<m.length()+1;j++){
StringBuilder temp=new StringBuilder(m);
for (int l = 0; l < 27; l++) {
StringBuilder temp2=new StringBuilder(temp.toString());
temp2.insert(j, (char)('a'+l));
String wanted = temp2.toString();
if (!added.contains(wanted)) {
if (hm.containsKey(wanted)) {
list.add(new Word(wanted, hm.get(wanted)));
added.add(wanted);
}
}
}
}
Collections.sort(list);
while(!list.isEmpty()){
sb.append(" ").append(list.remove().getWord());
}
sb.append("\n");
}
}
}
System.out.print(sb);
}
}class Word implements Comparable<Word>{
String word;
int index;
public Word(String word, int index) {
this.word = word;
this.index = index;
}
public int getIndex() {
return index;
}
public String getWord() {
return word;
}
@Override
public int compareTo(Word o) {
if(this.index>o.getIndex()){
return 1;
}
return -1;
}
}
No comments:
Post a Comment