-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva12460.java
More file actions
58 lines (55 loc) · 1.75 KB
/
Copy pathUva12460.java
File metadata and controls
58 lines (55 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
import java.io.*;
// bfs
public class Uva12460 {
static HashSet<String> set = new HashSet<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
String item = br.readLine();
while(!item.equals("--")){
set.add(item);
item = br.readLine() ;
}
while(br.ready()){
StringTokenizer st = new StringTokenizer(br.readLine());
String from = st.nextToken();
String to = st.nextToken();
if(from.length() != to.length()){
sb.append("No\n");
} else {
if( bfs(from.toCharArray() , to.toCharArray()) ){
sb.append("Yes\n");
} else {
sb.append("No\n") ;
}
}
}
pr.print(sb.toString()) ;
pr.close();
br.close();
}
static boolean bfs( char [] from , char [] to ){
String a = new String(from) ;
String b = new String(to) ;
if( a.equals(b) ){
return true ;
}
char [] out = Arrays.copyOf(from , from.length) ;
for (int i = 0; i < from.length; i++) {
if( from[i] != to[i] ){
out[i] = to[i] ;
String s = new String(out) ;
if( set.contains(s) ){
boolean state = bfs(out , to) ;
if( state ){
return true ;
}
}
out[i] = from[i] ;
}
}
return false ;
}
}