首页 > 专题系列 > Java解POJ > POJ 3356 AGTC [解题报告] Java
2013
11-12

POJ 3356 AGTC [解题报告] Java

AGTC

问题描述 :

Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:

  • Deletion: a letter in x is missing in y at a corresponding position.
  • Insertion: a letter in y is missing in x at a corresponding position.
  • Change: letters at corresponding positions are distinct

Certainly, we would like to minimize the number of all possible operations.

Illustration

A G T A A G T * A G G C

| | | | | | |
A G T * C * T G A C G C

Deletion: * in the bottom line
Insertion: * in the top line
Change: when the letters at the top and bottom are distinct

This tells us that to transform x = AGTCTGACGC into y = AGTAAGTAGGC we would be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should do it like

A  G  T  A  A  G  T  A  G  G  C

| | | | | | |
A G T C T G * A C G C

and 4 moves would be required (3 changes and 1 deletion).

In this problem we would always consider strings x and y to be fixed, such that the number of letters in x is m and the number of letters in y is n where nm.

Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.

Write a program that would minimize the number of possible operations to transform any string x into a string y.

输入:

The input consists of the strings x and y prefixed by their respective lengths, which are within 1000.

输出:

An integer representing the minimum number of possible operations to transform any string x into a string y.

样例输入:

10 AGTCTGACGC
11 AGTAAGTAGGC

样例输出:

4

解题代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main{

public static void main(String[] args) throws Exception{
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 String str ;
 while((str=in.readLine())!=null){
  StringTokenizer toke1 = new StringTokenizer(str);
  StringTokenizer toke2 = new StringTokenizer(in.readLine());
  int m = Integer.parseInt(toke1.nextToken());
  String str1 = toke1.nextToken();
  int n = Integer.parseInt(toke2.nextToken());
  String str2 = toke2.nextToken();
		
  int[][]array = new int[m+1][n+1];
		
  for(int i=0;i<=m;i++){
	array[i][0] = i;
  }
  for(int i=0;i<=n;i++){
	array[0][i] = i;
	}
		
  for(int i=1;i<=m;i++){
	for(int j=1;j<=n;j++){
	 if(str1.charAt(i-1)==str2.charAt(j-1))
	  array[i][j] = Math.min(Math.min(array[i-1][j-1], array[i-1][j]+1), array[i][j-1]+1);
	 else
	  array[i][j] = Math.min(Math.min(array[i-1][j-1]+1, array[i-1][j]+1), array[i][j-1]+1);
	}
	}
		
	System.out.println(array[m][n]);
	}
	}

}

  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }