2009-05-30 3 views
0

UVA의 Edit Step Ladders를 programming-challenges.com이라는 uva 하위 사이트에서 해결하고 있습니다. 그러나 테스트 입력을해야하는 모든 형식을 얻지 못했기 때문에 샘플 입력을 간단하게 가져 왔습니다. , 텍스트 파일에 넣고 거기에서 내 코드를 테스트했습니다. 이처럼 :온라인 판사를 준수하기 위해이 프로그램을 어떻게 적용합니까?

import java.io.*; 
import java.util.*; 

class Levenshtein { 
    private static int minimum(int a, int b, int c) { 
     if(a<=b && a<=c) 
      return a; 
     if(b<=a && b<=c) 
      return b; 
     return c; 
    } 

public static int computeLevenshteinDistance(String str1, String str2) { 
    return computeLevenshteinDistance(str1.toCharArray(), 
             str2.toCharArray()); 
} 

private static int computeLevenshteinDistance(char [] str1, char [] str2) { 
    int [][]distance = new int[str1.length+1][str2.length+1]; 

    for(int i=0;i<=str1.length;i++) 
      distance[i][0]=i; 

    for(int j=0;j<=str2.length;j++) 
     distance[0][j]=j; 

    for(int i=1;i<=str1.length;i++) 
     for(int j=1;j<=str2.length;j++) 
      distance[i][j]= minimum(distance[i-1][j]+1, 
            distance[i][j-1]+1, 
            distance[i-1][j-1]+ 
            ((str1[i-1]==str2[j-1])?0:1)); 

    return distance[str1.length][str2.length]; 
} 

public static void main(String args[]){ 

ArrayList<String> theWords = new ArrayList<String>(); 

try { 

     String ruta="entradaLevenshtein.txt"; 
     File myFile = new File (ruta); 
     FileReader fileReader = new FileReader(myFile); 

     BufferedReader reader = new BufferedReader(fileReader); 

     String line = null; 

     while ((line=reader.readLine())!=null){ 
      System.out.println(line); 
      theWords.add(line); 
     } 



     reader.close(); 
    } 

    catch (IOException ex){ 
     ex.printStackTrace(); 

    } 
{} 
// todo esto sólo para iniciar el arreglo 
// ahora vienen las llamadas a Levenstein y las comparaciones 

int maxEdit=0; 
int actualEdit=0; 

int wordsIndex1 =0, wordsIndex2=0; 


while (wordsIndex1<= theWords.size()) 
{ 
    while (wordsIndex2<= theWords.size()-1){ 
    actualEdit=computeLevenshteinDistance(theWords.get(wordsIndex1),theWords.get(wordsIndex2)); 
    if (actualEdit>maxEdit){maxEdit=actualEdit;} 
    wordsIndex2++; 
    } 
wordsIndex1++; 

} 

System.out.println(maxEdit+1); 
} 



} 

내 입력 파일의 존재는이 일이 캡처 어디 코드는 다음과 같은 패턴을 준수하도록되어있어

cat 
dig 
dog 
fig 
fin 
fine 
fog 
log 
wine 

, 일이 is..I은하지 않습니다 해당 문자열 :

import java.io.*; 
import java.util.*; 

class Modelo implements Runnable{ 
    static String ReadLn(int maxLength){ // utility function to read from stdin, 
              // Provided by Programming-challenges, edit for style only 
     byte line[] = new byte [maxLength]; 
     int length = 0; 
     int input = -1; 
     try{ 
      while (length < maxLength){//Read untill maxlength 
       input = System.in.read(); 
       if ((input < 0) || (input == '\n')) break; //or untill end of line ninput 
       line [length++] += input; 
      } 

      if ((input < 0) && (length == 0)) return null; // eof 
      return new String(line, 0, length); 
     }catch (IOException e){ 
      return null; 
     } 
    } 

    public static void main(String args[]) // entry point from OS 
    { 
     Modelo myWork = new Modelo(); // Construct the bootloader 
     myWork.run();   // execute 
    } 

    public void run() { 
     new myStuff().run(); 
    } 
} 
class myStuff implements Runnable{ 
    public void run(){ 
     try 
     { 

     /// PLACE YOUR JAVA CODE HERE 




     }catch(Exception e){ 
      System.out.println("A Exception was generated"); 
     } 
    } 

    // You can insert more classes here if you want. 
} 

왜 여기에 코드를 배치해야합니까?

try{ 
        while (length < maxLength){//Read untill maxlength 
         input = System.in.read(); 
         if ((input < 0) || (input == '\n')) break; //or untill end of line input 
         line [length++] += input; 

} 

어떻게 입력을 조작합니까 ??

답변

1

여기 아이디어는 run() 메서드에서 호출 될 myStuff 클래스에 프로그램을 작성하는 것입니다. run() 방법에서 Modelo.ReadLn()을 사용하여 입력 할 수 있습니다.

관련 문제