uva 164 String Computer(DP)
2014-02-19 15:45
531 查看
String Computer |
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]
相关文章推荐
- uva live 4394 String painter 间隔dp
- UVA 1437 String painter(区间dp)
- UVa 10739 String to Palindrome(经典回文串区间DP)
- UVA 10739 String to Palindrome(DP)
- UVa 10739 String to Palindrome (DP)
- uva 10739 String to Palindrome(dp)
- UVA 题目10739 String to Palindrome(区间DP)
- UVA 10739 String to Palindrome 转换回文 dp递推
- uva 10739 - String to Palindrome(区间dp)
- UVA 10739 String to Palindrome(dp)
- UVALive - 4394 String painter DP
- uva_10739 - String to Palindrome (普通DP)
- uva 11766 Racing Car Computer(DP)
- uva 10739 String to Palindrome (dp)
- uvalive 4394 string painter (序列dp)
- UVA 10739--String to Palindrome +dp
- uva live 4394 String painter 区间dp
- uva 11766 - Racing Car Computer(dp)
- String Partition - UVa 11258 dp
- UVa 10739 - String to Palindrome 字符串dp