Created
May 14, 2012 07:37
-
-
Save bknarendra/2692488 to your computer and use it in GitHub Desktop.
Codercharts:Word Lazy swap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class word_lazy_swap | |
{ | |
public static HashSet<String>dict; | |
public static void main(String[] args) throws Exception | |
{ | |
InputReader sc=new InputReader(new FileInputStream(args[0])); | |
dict=new HashSet<String>(); | |
while(!sc.isExhausted()) dict.add(sc.readLine()); | |
Queue<String>q=new LinkedList<String>(); | |
sc=new InputReader(new FileInputStream(args[1])); | |
String st="",end="",cur=""; | |
Set<String>used=new LinkedHashSet<String>(); | |
HashMap<String,String>back=new HashMap<String,String>(); | |
int flag=0,res=0; | |
List<String>suc; | |
while(!sc.isExhausted()) | |
{ | |
st=sc.readString(); | |
end=sc.readString(); | |
q.add(st); | |
while(!q.isEmpty()) | |
{ | |
cur=q.remove(); | |
suc=successors(cur); | |
for(int i=0;i<suc.size();i++) | |
{ | |
if(cur.equals(end)) | |
{ | |
flag=1; | |
break; | |
} | |
else | |
{ | |
if(!used.contains(suc.get(i))&&!q.contains(suc.get(i))) | |
{ | |
q.add(suc.get(i)); | |
back.put(suc.get(i),cur); | |
used.add(cur); | |
} | |
} | |
} | |
if(flag==1) break; | |
} | |
cur=end; | |
while(back.get(cur)!=null) | |
{ | |
cur=back.get(cur); | |
res++; | |
} | |
res++; | |
System.out.println(res); | |
used.clear(); | |
back.clear(); | |
q.clear(); | |
res=0;flag=0; | |
} | |
} | |
public static LinkedList<String>successors(String cur) | |
{ | |
StringBuffer sb; | |
LinkedList<String>suc=new LinkedList<String>(); | |
int n=cur.length(); | |
char ch; | |
for(int i=0;i<n;i++) | |
{ | |
sb=new StringBuffer(cur); | |
ch=sb.charAt(i); | |
for(char j='A';j<='Z';j++) | |
{ | |
if(ch!=j) | |
{ | |
sb.setCharAt(i,j); | |
if(dict.contains(sb.toString())) suc.add(sb.toString()); | |
sb.setCharAt(i,ch); | |
} | |
} | |
} | |
return suc; | |
} | |
} | |
class InputReader { | |
private boolean finished = false; | |
private InputStream stream; | |
private byte[] buf = new byte[1024]; | |
private int curChar; | |
private int numChars; | |
public InputReader(InputStream stream) { | |
this.stream = stream; | |
} | |
public int read() { | |
if (numChars == -1) | |
throw new InputMismatchException(); | |
if (curChar >= numChars) { | |
curChar = 0; | |
try { | |
numChars = stream.read(buf); | |
} catch (IOException e) { | |
throw new InputMismatchException(); | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar++]; | |
} | |
public int peek() { | |
if (numChars == -1) | |
return -1; | |
if (curChar >= numChars) { | |
curChar = 0; | |
try { | |
numChars = stream.read(buf); | |
} catch (IOException e) { | |
return -1; | |
} | |
if (numChars <= 0) | |
return -1; | |
} | |
return buf[curChar]; | |
} | |
public String readString() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
StringBuffer res = new StringBuffer(); | |
do { | |
res.appendCodePoint(c); | |
c = read(); | |
} while (!isSpaceChar(c)); | |
return res.toString(); | |
} | |
private boolean isSpaceChar(int c) { | |
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; | |
} | |
private String readLine0() { | |
StringBuffer buf = new StringBuffer(); | |
int c = read(); | |
while (c != '\n' && c != -1) { | |
if (c != '\r') | |
buf.appendCodePoint(c); | |
c = read(); | |
} | |
return buf.toString(); | |
} | |
public String readLine() { | |
String s = readLine0(); | |
while (s.trim().length() == 0) | |
s = readLine0(); | |
return s; | |
} | |
public String readLine(boolean ignoreEmptyLines) { | |
if (ignoreEmptyLines) | |
return readLine(); | |
else | |
return readLine0(); | |
} | |
public char readCharacter() { | |
int c = read(); | |
while (isSpaceChar(c)) | |
c = read(); | |
return (char) c; | |
} | |
public boolean isExhausted() { | |
int value; | |
while (isSpaceChar(value = peek()) && value != -1) | |
read(); | |
return value == -1; | |
} | |
public String next() { | |
return readString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment