Hduoj1503【DP】

来源:互联网 发布:python中执行adb命令 编辑:程序博客网 时间:2024/05/29 16:12
/*Advanced Fruits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 6   Accepted Submission(s) : 5Special Judge Problem DescriptionThe company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them. A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property. A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example. Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names.  InputEach line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters. Input is terminated by end of file.  OutputFor each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.  Sample Inputapple peachananas bananapear peach Sample Outputappleachbananaspearch SourceUniversity of Ulm Local Contest 1999*/#include<stdio.h>#include<string.h>char s1[110], s2[110];int dp[110][110], path[110][110], l1, l2;void print(int x, int y){if(!x && !y)return ;if( !x && y ){print(x, y-1);printf("%c", s2[y]);} if( x && !y){print(x-1, y);printf("%c", s1[x]);}if(path[x][y] == 1){print(x-1, y-1);printf("%c", s1[x]);}else if(path[x][y] == 2){print(x-1, y);printf("%c", s1[x]);}else if(path[x][y] == 3){print(x, y-1);printf("%c", s2[y]);}}int main(){int i, j, k;while(scanf("%s%s", s1+1, s2+1) != EOF){l1 = strlen(s1+1);l2 = strlen(s2+1);memset(dp, 0, sizeof(dp));for(i = 1; i <= l1; ++i){for(j = 1; j <= l2; ++j){if(s1[i] == s2[j])//如果当前位置的字符相同 {dp[i][j] = dp[i-1][j-1] + 1;//则最大匹配为上一个位置+1 path[i][j] = 1;//记录输出的字符为s1,s2的共同字符 }else//否则 {if(dp[i-1][j] > dp[i][j-1])//如果s1串的上一位置大于s2串的上一位置 {dp[i][j] = dp[i-1][j];//则令当前的最大匹配为s1的上一位置 path[i][j] = 2;//输出为s1串的当前字符 }else{dp[i][j] = dp[i][j-1];//令当前的最大匹配为s2的上一位置 path[i][j] = 3;//记录输出的字符为s2的当前字符 }}}}print(l1, l2);printf("\n");}return 0;}

题意:给出2个单词,现在要求将这2个单词合并,规则就是要求合并的新单词中必须都包含原来的2个字符串,并且这里的包含的意思是只要有原来的字符且相对顺序不改变即可。

思路:这里的意思变相的就是说寻找2个字符串所拥有的最长的公共子字符串,且不是连续的,这里用dp的思想去做,dp【i】【j】表示当前两个字符串的位置分别为i,j时所拥有的最大字符串匹配,由于要输出最终合并的字符串,所以我们用一个path【】【】记录当前输出的字符是属于s1的还是s2的,或是2者共同的。

0 0
原创粉丝点击