티스토리 툴바


알고리즘 테스트..

* 내용
* "A B C"가 복합 검색어로 입력되었을 때 요약할 부분을 찾는 프로그램을 작성하시오.
[문서 예]
     AxxxxxxxxxxxxxxxxAxxxxBxxxxxxAxxxxCxxxxxxCxAxxxxBxxxCxxxAxxxxxxxBxxxxC
 * 입력 코드
         int[] posA = {1, 18, 30, 44, 57};
         int[] posB = {23, 49, 65};
         int[] posC = {35, 42, 53, 70};
 * 출력 형식
      42,44에 있고 거리는 2이다.
* 힌트
* 문서의 A B C 의 각각의 거리가 필요함.


import java.util.*;

public class Exam1 {

static int k;

public static void main (String arg[]){ 

    long start = System.currentTimeMillis();

    int[] posA = new int[]{1, 18, 30, 44, 57};

    int[] posB = new int[]{23, 49, 65};

    int[] posC = new int[]{35, 42, 53, 70, 66};
     
    HashMap m = new HashMap();

    for(int i=0;i<=posA.length-1;i++){

       for(int j=0;j<=posB.length-1;j++){

          k = posA[i] - posB[j];

          m.put(Math.abs(k),"A에있는["+posA[i]+"]"+"B에있는["+posB[j]+"]");

       }

    }
       
    for(int i=0;i<=posA.length-1;i++){
      
       for(int j=0;j<=posC.length-1;j++){ 
     
          k = posA[i] - posC[j];

          m.put(Math.abs(k),"A에있는["+posA[i]+"]"+"C에있는["+posC[j]+"]");

       }

    }
      
    for(int i=0;i<=posB.length-1;i++){

       for(int j=0;j<=posC.length-1;j++){

          k = posB[i] - posC[j];

          m.put(Math.abs(k),"B에있는["+posB[i]+"]"+"C에있는["+posC[j]+"]");

       }

    }

    Collection cols = m.keySet();

    List temps = new ArrayList(cols);
      
    Collections.sort(temps);

    System.out.println((String)m.get((Integer)temps.get(0))+"에 있고 거리는 "+(Integer)temps.get(0)+"이다");

    long end = System.currentTimeMillis();

    System.out.println( "실행 시간 : " + ( end - start )/1000.0 );

    } 
 
}
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 Comment 0

DB 데이터변경 작업을 위해서 정규식 패턴을 이용한 connection pool.

package dbConnection;
import java.sql.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class updateDBData {
  public static void main(String [] args){
    Connection con = null;
    Statement stmt = null;
    Statement stmt1 = null;
    ResultSet rs = null;   
    String sql="";
   
    try{
     Class.forName("org.gjt.mm.mysql.Driver");
    }catch(ClassNotFoundException e){
     System.out.println(e.getMessage());
    }
   
    try{
      String url = "jdbc:mysql://이하db주소";
      con = DriverManager.getConnection(url,"암호","패스워드");
      //System.out.println("연결성공");   
      try{
          stmt = con.createStatement();
      } catch (SQLException ee){
          System.err.println("Statement Create Failed.");
      }
      try{         
          rs = stmt.executeQuery("select content,pstcrt_id,pst_id from tb_posting_correct where content is not null and content like '%errorType%'");                   
          while (rs.next()){                         
          String content = rs.getString("content");        
          String pstctr_id = rs.getString("pstcrt_id");
          String pst_id = rs.getString("pst_id");         
          Pattern p = Pattern.compile("(errorType=\")([^\\p{Space}\\>\\\"]+)");         
          /*'errorType="'로 시작을 하고 스페이스, '>', 큰 따옴표가 나오기 전까지의 모든 문자들의 연속을 찾습니다.
           * ()괄호로 묶음은 그룹을 뜻하는데 여기서 그룹 1 은 'errorType="' 이며,
           * 그룹 2 는 errorType=" 뒤 문자부터 쌍따옴표가 닫히는 전부분의 문자를 의미 합니다.
           * 마지막 쌍따옴표가 있는 부분을 패턴매칭을 하기 위해서 앞에 \\(역슬래쉬2개)를 해주어야 합니다.           
           */
          Matcher m = p.matcher(content);
              while (m.find()){
             String afterContent = m.group(2);             
             String tmpContent = "";             
             if(afterContent.charAt(0)=='Y'){
              tmpContent +=  "adj|";
             }if(afterContent.charAt(1)=='Y'){
              tmpContent += "adv|";
             }if(afterContent.charAt(2)=='Y'){
              tmpContent += "cap|";
             }if(afterContent.charAt(3)=='Y'){
              tmpContent += "cms|";
             }if(afterContent.charAt(4)=='Y'){
              tmpContent += "cjc|";
             }if(afterContent.charAt(5)=='Y'){
              tmpContent += "ics|";
             }if(afterContent.charAt(6)=='Y'){
              tmpContent += "icvf|";
             }if(afterContent.charAt(7)=='Y'){
              tmpContent += "ifn|";
             }if(afterContent.charAt(8)=='Y'){
              tmpContent += "nps|";
             }if(afterContent.charAt(9)=='Y'){
              tmpContent += "psf|";
             }if(afterContent.charAt(10)=='Y'){
              tmpContent += "pps|";
             }if(afterContent.charAt(11)=='Y'){
              tmpContent += "pnc|";
             }if(afterContent.charAt(12)=='Y'){              
              tmpContent += "pca|";
             }if(afterContent.charAt(13)=='Y'){
              tmpContent += "rwop|";
             }if(afterContent.charAt(14)=='Y'){
              tmpContent += "stv|";
             }if(afterContent.charAt(15)=='Y'){
              tmpContent += "spl|";
             }if(afterContent.charAt(16)=='Y'){
              tmpContent += "sva|";
             }if(afterContent.charAt(17)=='Y'){
              tmpContent += "vob|";
             }if(afterContent.charAt(18)=='Y'){
              tmpContent += "abb|";
             }if(afterContent.charAt(19)=='Y'){
              tmpContent += "arc|";
             }if(afterContent.charAt(20)=='Y'){
              tmpContent += "atc|";
             }if(afterContent.charAt(21)=='Y'){
              tmpContent += "cls|";
             }if(afterContent.charAt(22)=='Y'){
              tmpContent += "clq|";
             }if(afterContent.charAt(23)=='Y'){
              tmpContent += "cpsl|";
             }if(afterContent.charAt(24)=='Y'){
              tmpContent += "cst|";
             }if(afterContent.charAt(25)=='Y'){
              tmpContent += "dbn|";
             }if(afterContent.charAt(26)=='Y'){
              tmpContent += "els|";
             }if(afterContent.charAt(27)=='Y'){
              tmpContent += "edp|";
             }if(afterContent.charAt(28)=='Y'){
              tmpContent += "jrg|";
             }if(afterContent.charAt(29)=='Y'){
              tmpContent += "lst|";
             }if(afterContent.charAt(30)=='Y'){
              tmpContent += "nbu|";
             }if(afterContent.charAt(31)=='Y'){
              tmpContent += "ovs|";
             }if(afterContent.charAt(32)=='Y'){
              tmpContent += "pnn|";
             }if(afterContent.charAt(33)=='Y'){
              tmpContent += "rdd|";
             }if(afterContent.charAt(34)=='Y'){
              tmpContent += "rtp|";
             }if(afterContent.charAt(35)=='Y'){
              tmpContent += "ros|";
             }if(afterContent.charAt(36)=='Y'){
              tmpContent += "smw|";
             }if(afterContent.charAt(37)=='Y'){
              tmpContent += "sif|";
             }if(afterContent.charAt(38)=='Y'){
              tmpContent += "spw|";
             }if(afterContent.charAt(39)=='Y'){
              tmpContent += "tss|";
             }if(afterContent.charAt(40)=='Y'){
              tmpContent += "tdm|";             
             }if(afterContent.charAt(41)=='Y'){
              tmpContent += "vga|";
             }
             content.replaceAll(afterContent, tmpContent);
             m.replaceAll("errorType=\"" + tmpContent);             
             stmt1 = con.createStatement();
             sql ="update tb_posting_correct set content=\""+m.replaceAll("errorType=\"" + tmpContent).replaceAll("\"","\\\\\"")+"\" where pstcrt_id="+pstctr_id+" and pst_id="+pst_id+";";
             stmt1.executeUpdate(sql);             
          }
             
         }      
      }catch (SQLException ee){    
          System.err.println("Query Send Failed" + ee.toString());
      }
    }catch(SQLException e){
      System.out.println(e.getMessage());
    }finally{
       try{
       if(rs != null) rs.close();      
       if(stmt != null) stmt.close();
       if(stmt1 != null) stmt1.close();
       if(con != null) con.close();
       }catch (SQLException e){      
        e.printStackTrace();
       }
   }
  }
}

위의 소스는 Mysql DB 데이터변경 작업을 위해서 정규식 패턴을 이용해서 만들어본 소스이다.
tb_posting_correct를 select쿼리로 pstcrt_id, pst_id, content를 뽑아낸다.
결과값의 content에 errorType이 있는지 비교를 한다. (errorType="YYNNNNNYYNNNNNNNNNNNNNNNNYYY" 와 같이 값이 들어가 있었음.) 없으면 다음으로 넘어가고 있으면 YNN과 같은 값을 인덱스 값으로 비교 char를 치환한다.
update를 한다.
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 Comment 0

봄에서 만든 첨삭 툴




입사 이후 계속 만들어 나가고 있는 첨삭 툴 프로그램.
이젠 어느정도 모습이 갖추어 가고 있다.
아직도 조금 모자란 부분이 없지 않아 있지만 그래도 어느덧 멋진 프로그램이 되었다.
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 Comment 0
prev 1 2 3 4 5 ... 11 next