每日一九度之 题目1042:Coincidence

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3007

解决:1638

题目描述:

Find a longest common subsequence of two strings.

输入:

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.

输出:

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

样例输入:
abcd
cxbydz
样例输出:
2

经典的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;
}
复制代码

 

posted @   Asimple  阅读(242)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示