poj 2250 Compromise(裸LCS DP)

华为昇腾Atlas 200I DK A2实战:从开箱到运行首个AI应用 本文详细介绍了华为昇腾Atlas 200I DK A2开发板从开箱到运行首个AI应用的全过程。包括硬件准备、系统烧录、开发板连接方案、AI应用运行及性能优化技巧,帮助开发者快速上手部署AI项目,提升开发效率。 阅读详情

Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6766 Accepted: 3023 Special Judge

Description

In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do. 

Therefore the German government requires a program for the following task: 
Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind). 

Your country needs this program, so your job is to write it for us.

Input

The input will contain several test cases. 
Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'. 
Input is terminated by end of file.

Output

For each test case, print the longest common subsequence of words occuring in the two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.

Sample Input

die einkommen der landwirte
sind fuer die abgeordneten ein buch mit sieben siegeln
um dem abzuhelfen
muessen dringend alle subventionsgesetze verbessert werden
#
die steuern auf vermoegen und einkommen
sollten nach meinung der abgeordneten
nachdruecklich erhoben werden
dazu muessen die kontrollbefugnisse der finanzbehoerden
dringend verbessert werden
#

Sample Output

die einkommen der abgeordneten muessen dringend verbessert werden

题目链接:http://poj.org/problem?id=2250


题目大意:求出两段文字中最大公共单词序列,最大公子序列LCS问题。


解题思路:LCS模板:dp[i][j]记录前a[i]和前b[j]的最大公共子序列长度。若要记录下子序列,需要数组path[i][j];

分为三种情况:1.a[i-1]==b[j-1]  :  dp[i][j]=dp[i-1][j-1]+1,path[i][j]=0;

 2.dp[i-1][j]>dp[i][j-1] :  dp[i][j]=dp[i-1][j]  ,path[i][j]=1;

 3.dp[i][j-1]>dp[i-1][j] :  dp[i][j]=dp[i][j-1]  ,path[i][j]=-1; 

ans[i]数组记录子序列,path[i][j]=0: ans[cnt++]=a[i];

   path[i][j]=1: i--;

   path[i][j]=-1: j--;

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
string a[105],b[105];
string ans[105];
int path[105][105],dp[105][105];
int la,lb,t,l;
void get(int i,int j)
{
    if(!i||!j)
        return;
    if(path[i][j]==0)
    {
        get(i-1,j-1);
        ans[l++]=a[i-1];
    }
    else if(path[i][j]==1)
        get(i-1,j);
    else
        get(i,j-1);
}
int main()
{
    la=lb=t=l=0;
    string s;
    memset(path,0,sizeof(path));
    memset(dp,0,sizeof(dp));
    while(cin>>s)
    {
        if(t==0)
        {
            a[la++]=s;
            while(cin>>s&&s!="#")
                a[la++]=s;
            t=1;
            continue;
        }
        if(t==1)
        {
            b[lb++]=s;
            while(cin>>s&&s!="#")
                b[lb++]=s;
        }
        for(int i=1;i<=la;i++)
            for(int j=1;j<=lb;j++)
            {
                if(a[i-1]==b[j-1])
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                    path[i][j]=0;
                }
                else if(dp[i-1][j]>dp[i][j-1])
                {
                    dp[i][j]=dp[i-1][j];
                    path[i][j]=1;
                }
                else
                {
                    dp[i][j]=dp[i][j-1];
                    path[i][j]=-1;
                }
            }
        get(la,lb);
        for(int i=0;i<l-1;i++)
            cout<<ans[i]<<" ";
        cout<<ans[l-1]<<endl;
        la=lb=l=t=0;
        memset(dp,0,sizeof(dp));
        memset(path,0,sizeof(path));
    }
    return 0;
}


CMake教程:C++ CMake自定义find_package并在其它项目使用 find_package 是 CMake 中一个非常重要的命令,用于在系统中找到外部库或包,并将其包含到项目中。: 要查找的包的名称。[version]: 可选的版本号。如果指定了版本号,CMake 会尝试找到该版本或更高版本的包。[REQUIRED]: 如果指定了这个关键字,找不到包时会产生一个错误并终止配置过程。: 指定要查找的包的组件。: 指定可选的组件,即使找不到这些组件,配置过程也会继续。: 指定查找包的模式,CONFIG表示查找配置文件,MODULE表示查找模块文件。 阅读详情

相关推荐

SAP PP工艺路线Routing保姆级配置指南:从CA01创建到替代/并行顺序实战

本文详细解析SAP PP模块中工艺路线(Routing)的配置与应用,从基础创建(CA01)到高级功能如替代顺序和并行顺序的实战操作。通过真实案例演示,帮助用户掌握SAP PP工艺路线的精细化管理,提升生产流程效率。

weixin_30898555的博客 397

A bunch of tech news related to China from Yahoo

lets see whats the recently hot topics in Yahoo news this week. about China, we get 3 news here.   Google.cn is now struggling to filter pornography images from its search engine since the Chine

陈敏的博客 3522

EMC(6)——电磁场的屏蔽

假如,机箱没有开口缝隙,比如篮球这种壳体,屏蔽效能都能达到100dB,100dB,基本上把电磁场强度能衰减万倍的关系,所以,这种方式屏蔽效能非常高。一般,钣金机箱,缝隙很大,大家目视就可以看到,铝铣的机箱,比较平整,但实际情况,表面其实也是坑坑洼洼,只是,大家肉眼看不出来而已!这种安装方式,是直接在缝隙增加导电材料,存在一个问题,就是,手法温柔点,压缩度会不够,手法粗暴点,过度压缩。所以,不是说,用金属机箱就能起到屏蔽效果,重要的是,要做好机箱孔缝电磁兼容设计,否则,是金属机箱,也是摆设。

qq_39825125的博客 2195

YouPorn - Targeting 200 Million Views A Day And Beyond

Erick Pickup, lead developer at YouPorn.com, presented their architecture in a talk titledBuilding a Website To Scale given at the ConFoo conference.  As you might expect, YouPorn is a beast, streamin...

weixin_34192816的博客 58万+

Win11下Android模拟器报HAXM未安装?三步搞定虚拟化配置(附终端命令)

本文详细解析了Win11下Android模拟器HAXM报错的解决方案,从虚拟化配置到终端命令实战。通过检查BIOS设置、调整Windows功能及执行PowerShell命令,三步搞定虚拟化配置问题,有效提升模拟器性能。特别针对Win11与HAXM的兼容性问题提供了专业指导。

weixin_29215815的博客 149

用 Python 和 XGBoost 进行疾病诊断预测

疾病诊断预测是医疗领域的重要研究方向,准确的疾病诊断预测可以帮助医生提前制定治疗方案,提高治疗效果,降低医疗成本。本文章的目的是介绍如何使用 Python 和 XGBoost 算法构建疾病诊断预测模型。范围涵盖了从数据预处理、模型训练到模型评估的整个流程,同时通过实际案例展示如何将该模型应用于疾病诊断预测中。本文首先介绍了疾病诊断预测的背景和 XGBoost 算法的核心概念,然后详细讲解了 XGBoost 算法的原理和具体操作步骤,包括 Python 代码实现。

AI Python 编程之道的博客 1066

How YouPorn Uses Redis: SFW Edition

I interviewed Eric Pickup, IT Lead at the Manwin group (the company behind sites like YouPorn and Pornhub), to tell us about their transition to using Redis, why they made the switch, and how well i

cnbird's blog 11万+

【多目标跟踪】ByteTrack复现(有yolov5例子)

1.环境 ubuntu16.04 cuda10.1 cudnn7 python3.6 Cython matplotlib>=3.2.2 numpy>=1.18.5 opencv-python>=4.1.2 Pillow PyYAML>=5.3 scipy>=1.4.1 tensorboard>=2.2 torch>=1.7.0 torchvision>=0.8.1 tqdm>=4.41.0 seaborn>=0.11.0 easydic

聿默的博客 1万+

How I Made Porn 20x More Efficient with Python

原文:http://www.toptal.com/python/how-i-made-porn-20x-more-efficient-with-python 译文:http://blog.jobbole.com/39323/ Porn is a big industry.There aren’t many sites on the Internet that can riv

.. 3万+

【阿里AgentScope框架】多框架组合:AgentScope加LangChain,让你的开发效率直线上升

本文以一个例子来看下AgentScope框架和LangChain如何实现融合使用,真的超级简单,一看就懂。这是一种思路,为多种框架的融合使用提供借鉴。

同学小张的博客 3496

llama-factory微调qwen2.5-vl

使用llama-factory完成qwen2.5-vl微调

qq_44908396的博客 2940

Poj 2250 Compromise

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4965   Accepted: 2266   Special Judge Description In a few months the European Currency Unio

Paradiser 806

反爬虫:POST 请求参数中 Sign 的加密机制详解,并设计带 Sign 校验的网站

本文介绍了API请求中sign签名的生成原理及实现方法。sign是通过对请求参数加密生成的一串字符串,用于验证请求合法性,防止参数篡改、重放攻击和伪造请求。其生成步骤包括参数排序、拼接字符串、加盐和哈希加密。文章还演示了如何在FastAPI后端实现带sign验证的新闻网站,包括参数校验、时间戳验证和签名比对等安全措施,有效提升API安全性,增加爬虫逆向难度。

数据知道的博客 4086

POJ 2250 Compromise(最长公共子序列LCS)

POJ 2250 Compromise(最长公共子序列LCS) http://poj.org/problem?id=2250 题意: 给你两段由空格分隔的语句, 要你求该两段语句的最长公共子序列. 且随便输出一个解即可. 注意每个单词需要看成我们一般处理字符串子序列的一个单独字符. 即每个单词是一个整体. 分析: 与往常计算最长公共子序列一样的方式即可. 然后用DFS输出序列即可.本题与POJ1458提供的解法本质一样. http://blog.csdn.net/u0134806

code 2114

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

poj 2250 Compromise (DP-LCS 记录路径)

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6539   Accepted: 2929   Special Judge Description In a few months the European Currency Union wi

GODLINESS_ 553

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

poj-2250 Compromise(LCS)

Compromise Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10548 Accepted: 4499 Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled,

weixin_47750287的博客 235

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

POJ2250:Compromise(LCS)

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (

ACM!荣耀之路! 3092

【POJ 2250】Compromise(最长公共子序列LCS)

题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出。 #include<cstdio> #include<cstring> #define N 102 #define M 32 int n,m,dp[N][N],ans[N][N]; char s1[N][M],s2[N][M],str[N]...

dingguayi7025的博客 142

poj 2250 Compromise

题目链接:http://poj.org/problem?id=2250 poj 的数据好水啊! #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define inf 0x3f3f3f3f #define

Wings_of_Liberty的专栏 465

poj 2250 Compromise

#include #include #include using namespace std; char a[35][105],b[35][105],c[35][105]; int dp[105][105],mark[105][105],len1,len2,cnt; void LCS() { int i,j; memset(dp,0,sizeof(dp)); me

wuhulala的休息室 671

poj2250 Compromise dp lcs 记录路径

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5045   Accepted: 2297   Special Judge Description In a few months the European Currency Union will

Shirley 1642

POJ 2250 Compromise 最长公共子序列LCS 动态规划DP

http://poj.org/problem?id=2250 最长公共子序列LCS 动态规划DP Compromise Time Limit:1000MS Memory Limit:65536K Special Judge Description In a few months the European Currency Unio...

soboer 93

POJ ——2250 Compromise 【LCS的记录路径】

题意大致就是给你两个文本,文本里面有很多单词,问你这个两个文本中相同的单词个数最大是多少,输出路径; 这道题跟LIS最大的不同就是LIS是对一个串进行处理,所以可以使用一个path数组来进行记录路径,而LCS则是两个不完全相同的文本,无法使用path数组来记录某一个单词的前驱位置,所以这里使用了dfs递归搜索,这也是给我所学习到的一点,之前都是把dfs当成一种搜图工具,没有了解本质;只不过是一个...

HPU WIN 271

POJ2250:Compromise(LCS) 解题心得

原题: Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for t...

advyne9986的博客 191

poj-2250Compromise(LCS+标记数组)

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7332   Accepted: 3229   Special Judge Description In a few months the European Currency Union

Eric_keke的专栏 684

POJ 2250 Compromise (线性dp LCS +递归路径)

POJ 2250 Compromise (线性dp LCS +递归路径)

Tc的专栏 1254
上一篇: hdu 1506 Largest Rectangle in a Histogram (DP)
下一篇: LightOJ 1231 Coin Change (I) (背包计数模板题)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址