您的位置:首页 > 其它

uva 164 String Computer(DP)

2014-02-19 15:45 531 查看



String Computer

Extel have just brought out their newest computer, a string processing computer dubbed the X9091. It is hoped that it will have some value in cryptography and related fields. (It is rumoured that the Taiwanese are working on a clone that will correct Stage
1 essays, but we will ignore such vapourware). This computer will accept input strings and produce output strings from them, depending on the programs loaded into them at the time. The chip is the ultimate in RISC technology--it has only three transformation
instructions:

Delete a character at a particular position.
Insert a character at a particular position.
Change the character at a particular position to a different character.

Programs for this machine are written in a form of machine code where each instruction has the formatZXdd--Z represents the code for the instruction (D,
I orC), X is a character and dd represents a two digit number. A program is terminated by a special halt instruction consisting of the letter `E'. Note that each instruction works on the string in memory at the time
the instruction is executed.

To see how this all works consider the following example. It is desired to transform the string `abcde' to the string `bcgfe'. This could be achieved by a series of Change commands, but is not minimal. The following program is better.



Write a program that will read in two strings (the input string and the target string) and will produce aminimal X9091 program necessary to transform the input string into the target string. Since there may be multiple solutions, only one should
be produced. Any solution that satisfies these criteria will be accepted.

Input and Output

Input will consist of a series of lines, each line containing two strings separated by exactly one space. The strings will consist of no more than 20 lower case characters. The file will be terminated by a line consisting of a single#.

Output will consist of a series of lines, one for each line of the input. Each will consist of a program in X9091 language.

Sample input

abcde bcgfe
#


Sample output

Da01Cg03If04E


记忆化:dp[i][j] = min(DP(i+1 , j+1)(Change) , min(DP(i+1 , j)(Delelte) , DP(i , j+1)(Insert)));

op[i][j]记录路径!


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

const int maxn = 30;
int dp[maxn][maxn] , op[maxn][maxn] , len1 , len2;
string ini , dis;

void initial(){
for(int i = 0 ; i < maxn ; i++){
for(int j = 0 ; j < maxn ; j++){
dp[i][j] = -1;
op[i][j] = 0;
}
}
len1 = ini.length();
len2 = dis.length();
}

int DP(int i , int j){
if(i >= len1) return len2-j;
if(j >= len2) return len1-i;
if(dp[i][j] != -1) return dp[i][j];
if(DP(i+1, j) < DP(i , j+1)){
op[i][j] = 3;
dp[i][j] = DP(i+1 , j)+1;
}else{
op[i][j] = 2;
dp[i][j] = DP(i , j+1)+1;
}
if(ini[i] == dis[j]){
if(DP(i+1 , j+1) <= dp[i][j]){
op[i][j] = 0;
dp[i][j] = DP(i+1 , j+1);
}
}else{
if(DP(i+1 , j+1)+1 <= dp[i][j]){
op[i][j] = 1;
dp[i][j] = DP(i+1 , j+1)+1;
}
}
return dp[i][j];
}

void out(int i , int j , int p){
if(i >= len1){
while(j < len2){
printf("I%c%02d" , dis[j++] , p++);
}
printf("E\n");
return;
}
if(j >= len2){
while(i < len1){
printf("D%c%02d" , ini[i++] , p);
}
printf("E\n");
return;
}
if(op[i][j] == 0){
out(i+1 , j+1 , p+1);
}else if(op[i][j] == 1){
printf("C%c%02d" , dis[j] , p);
out(i+1 , j+1 , p+1);
}else if(op[i][j] == 2){
printf("I%c%02d" , dis[j] , p);
out(i , j+1 , p+1);
}else{
printf("D%c%02d" , ini[i] , p);
out(i+1 , j , p);
}
}

void computing(){
DP(0 , 0);
out(0 , 0 , 1);
}

int main(){
freopen("in" , "r" , stdin);
while(cin >> ini && ini != "#"){
cin >> dis;
initial();
computing();
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: