每日一九度之 题目1042:Coincidence

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3007

解决:1638

<dl> <dt> 题目描述: </dt> <dd>

Find a longest common subsequence of two strings.

</dd> </dl> <dl> <dt> 输入: </dt> <dd>

First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100.

</dd> </dl> <dl> <dt> 输出: </dt> <dd>

For each case, output k – the length of a longest common subsequence in one line.

</dd> </dl> <dl> <dt> 样例输入: </dt> <dd>
abcd
cxbydz
</dd> </dl> <dl> <dt> 样例输出: </dt> <dd>
2
</dd> </dl>

经典的dp问题。LCS。相当于dp的入门级题目吧!

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#define INF 0x7fffffff
using namespace std;
const int maxn = 115;
typedef long long ll;
int n, num;
char s1[maxn], s2[maxn];
int dp[maxn][maxn];

int main(){
    while( cin >> s1 >> s2 ){
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        for (int i=0; i<=len1; ++i)  
            dp[i][0] = 0;  
       for (int j=0; j<=len2; ++j)  
            dp[0][j] = 0; 
        for(int i=1; i<=len1; i++){
            for(int j=1; j<=len2; j++){
                if( s1[i-1] == s2[j-1] ){
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        printf("%d\n",dp[len1][len2]);
    }
    return 0;
}

 

全部评论

相关推荐

KPLACE:首先是板面看起来不够,有很多奖,比我厉害。项目要精减,大概详细描述两到三个,要把技术栈写清楚,分点,什么算法,什么外设,怎么优化,不要写一大堆,分点,你写上去的目的,一是让别人知道你做了这个知识点,然后在面试官技术面的时侯,他知道你会这个,那么就会跟你深挖这个,然后就是个人评价改为专业技能
点赞 评论 收藏
分享
什么时候才能有offer啊_:十年前我还在刺激战场研究跳伞的底层原理呢
投递牛客等公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务