UVa 531 Compromise:DP&LCS

531 - Compromise

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=472

注意用一个全局变量flag来确定是否输出空格。

完整代码:

/*0.029s*/

#include<bits/stdc++.h>
using namespace std;  

char a[105][35], b[105][35];
int dp[105][105], path[105][105];
bool first;///建议用全局变量调整空格的输出与否  

void printlcs(int i, int j)
{
    if (i == 0 || j == 0) return;
    if (path[i][j] == 1)
    {
        printlcs(i - 1, j - 1);
        if (first)
        {
            printf("%s", a[i]);
            first = false;
        }
        else printf(" %s", a[i]);
    }
    else if (path[i][j] == 2) printlcs(i - 1, j);
    else printlcs(i, j - 1);
}  

int main()
{
    int lena, lenb, i, j;
    while (~scanf("%s", a[1]))
    {
        if (a[1][0] != '#')
            for (lena = 2; scanf("%s", a[lena]), a[lena][0] != '#'; ++lena)
                ;
        for (lenb = 1; scanf("%s", b[lenb]), b[lenb][0] != '#'; ++lenb)
            ;
        memset(dp, 0, sizeof(dp));
        for (i = 1; i < lena; ++i)
            for (j = 1; j < lenb; ++j)
            {
                if (strcmp(a[i], b[j]) == 0)
                    dp[i][j] = dp[i - 1][j - 1] + 1, path[i][j] = 1;
                else if (dp[i - 1][j] >= dp[i][j - 1])
                    dp[i][j] = dp[i - 1][j], path[i][j] = 2;
                else
                    dp[i][j] = dp[i][j - 1], path[i][j] = 3;
            }
        first = true;
        printlcs(lena - 1, lenb - 1);
        putchar(10);
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索dp
, int
, path
, 空格
, else
first
,以便于您获取更多的相关知识。

时间: 2024-08-31 08:03:57

UVa 531 Compromise:DP&amp;LCS的相关文章

UVa 10192 Vacation:DP&amp;amp;LCS

10192 - Vacation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1133 水. 完整代码: /*0.019s*/ #include<bits/stdc++.h> using namespace std; char a[105],

UVa 10066 The Twin Towers:DP&amp;amp;LCS

10066 - The Twin Towers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1007 水. 完整代码: /*0.012s*/ #include<bits/stdc++.h> using namespace std; int a

UVa 11137 Ingenuous Cubrency (DP)

11137 - Ingenuous Cubrency Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2078 People in Cubeland use cubic coins. Not only the unit of currency is called acube but also

UVa 674 Coin Change (DP)

674 - Coin Change Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=615 Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent.

UVa 10003 Cutting Sticks (DP)

10003 - Cutting Sticks Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=944 You have to cut a wood stick into pieces. The most affordable company, The Ana

UVa 147 Dollars:经典DP&amp;amp;硬币组合数&amp;amp;整数拆分

147 - Dollars Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 和UVa 357一样. 注意所有数除以5再算. 完整代码: /*0.019s*/ #include<cstdio> #include<cstring> const int coin[11

UVa 10313 Pay the Price:DP&amp;amp;整数拆分

10313 - Pay the Price Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1254 In ancient days there was a country whose people had very interesting habits.

UVa 437 The Tower of Babylon:DP&amp;amp;DAG

437 - The Tower of Babylon Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=378 思路: 对于一个(x,y,z)砖头,它可以有3中姿势放置: (前两个为地面,后一个为高) x, y, z x, z, y y, z, x 把每种姿势

UVa 10651 Pebble Solitaire:DP&amp;amp;bitset

10651 - Pebble Solitaire Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1592 化为二进制进行状态转移,详见代码. 完整代码: /*0.016s*/ #include<bits/stdc++.h> using names