Compromise (p2250)

本文介绍了一种解决最长公共子序列问题的算法实现方法,通过动态规划的方式找到两个文本间的最长公共子序列,并详细展示了如何回溯得到具体解。适用于处理如政治提案等文本的相似性比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第二次做,不过一开始没有看清会是多组数据,都搞得我都不知道哪错了。

这个和找公共子序列是一个道理(把一个单词看成是一个字母)。输出的路径进行记录,pre[][],其中3代表是左上,2代表左边,1代表正上方



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<string>

using namespace std;

//freopen("C://i.txt","r",stdin);


#define N 1001

char a[111][33];
char b[111][33];
int lena,lenb;
int dp[111][111];

int 
 pre[111][111];
bool f;

void put(int i,int j)
{
//	cout<<i<<' '<<j<<' '<<pre[i][j]<<endl;
	int k;
	if (i==0||j==0)
		return ;
	
	
	if (pre[i][j]==3)
		put(i-1,j-1);
	else if (pre[i][j]==1)
		put(i,j-1);
	else if (pre[i][j]==2)
		put(i-1,j);
	
	if (strcmp(a[i],b[j])==0) //这里要加强i,j不然有空格 
	{
		if (f)
		cout<<' ';
		f=true;
		cout<<a[i];
	}
}

int main()
{
	freopen("C://i.txt","r",stdin);
	int i,j,k;
	while (cin>>a[1])
	{
		

	lena=2;
	f=false;
	while (cin>>a[lena])
	{
		//cout<<a[lena]<<endl;
		if (a[lena][0]=='#')
			break;
		else
			lena++;
	}
	lena--;
	lenb=1;
	while (cin>>b[lenb])
	{
		//cout<<b[lenb]<<endl;
		if (b[lenb][0]=='#')
			break;
		else
			lenb++;
	}
	lenb--;
	int ansi,ansj;
	
	ansi=0;
	ansj=0;

	memset(pre,0,sizeof(pre));
	
	for (i=0;i<=lena;i++)
	{
		for (j=0;j<=lenb;j++)
		{
			if (i==0||j==0)
			{
				dp[i][j]=0;
			}
			else if (strcmp(a[i],b[j])==0)
			{
			//	cout<<i<<' '<<j<<endl;
				dp[i][j]=dp[i-1][j-1]+1;
				pre[i][j]=3;
			}
			else 
			
			{
				if (dp[i-1][j]>dp[i][j-1])
				{
					dp[i][j]=dp[i-1][j];
					pre[i][j]=2;
				}
				else
				{
					dp[i][j]=dp[i][j-1];
					pre[i][j]=1;
				}
			}
		}
	}
	
//	cout<<dp[lena][lenb]<<endl;
//	put(lena,lenb);
	
	char ans[111][33];
	k=0;
	
	put(lena,lenb);
	cout<<endl;
	/*
	i=lena;
	j=lenb;
	while (pre[i][j])
	{
		int q,p;
		q=i;
		p=j;
		if (pre[i][j]==3)
			p--,q--;
		else if (pre[i][j]==2)
			q--;
		else
			p--;
		
		if (dp[i][j]!=dp[q][p])
			strcpy(ans[k++],a[i]);
			
		i=q;
		j=p;	
	}
	
	for (i=k-1;i>=0;i--)
		printf("%s ",ans[i] );
	printf("\n");
	*/
	}
	
} 


Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4814 Accepted: 2205 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

Source



优化此SQL,语法为PostgreSQL:select case when (pct1.id is not null and rnu.customer_submit_flag = 0) then pct1.ID else pct.ID end id, pct.regist_no registNo, pct.license_no licenseNo, prm.customer_name customerName, prm.customer_mobile customerMobile, prm.report_time reportTime, prm.customer_no customerNo, prm.repair_fee repairFee, prm.repair_type repairType, prm.shop_name shopName, prm.cancel_push_reason cancelPushReason, prm.repair_rule repairRule, prms.license_no otherLicenseNo, prms.repair_fee otherRepairFee, prms.repair_type otherRepairType, prms.shop_name otherShopName, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then pct1.last_in_time else pct.last_in_time end lastInTime, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then (case when pct1.ext2 is not null then pct1.ext2 else pct1.last_out_time end) else (case when pct.ext2 is not null then pct.ext2 else pct.last_out_time end) end lastOutTime, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then pct1.last_out_time else pct.last_out_time end overTime, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then d1.username else d.username end conductorUsername, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then d1.usercode else d.usercode end conductorUsercode, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then substr(d1.comcode, 1, 4) else substr(d.comcode, 1, 4) end conductorComname, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then '110' else '100' end caseType, prm.status status, rm.regist_type registType, case when (pct1.id is not null and rnu.customer_submit_flag = 0) then pct1.out_flag else pct.out_flag end outFlag, case when rm.xers_flag = '1' then '是' else '否' end xersFlag, case when prm.compromise = '1' then '是' else '否' end compromise, case when prm.disaster_marker = '1' then '是' else '否' end disasterMarker, case when prm.newenergy_flag = '0' then '否' else '是' end disasterMarker, prm.offline_type, substr( prm.damage_comcode, 1, 4 ) damageComcode, substr( rm.policy_comcode, 1, 4 ) policyComcode, prst.twice_flag twiceFlag, prst.dealer_name dealerName, prst.dealer_code dealerCode, prst.twice_result twiceResult, prst2.dealer_code firstDealerCode, prst2.dealer_name firstDealerName from picc_report_msg prm left join picc_case_task pct on pct.regist_id = prm.regist_id and pct.case_type = 100 left join picc_case_task pct1 on pct1.regist_id = prm.regist_id and pct1.case_type = 110 left join picc_report_surveytwice prst on prm.id = prst.msg_id and prst.loss_item = '050' and prst.twice_flag = '1' left join (select prst3.msg_id msg_id,max(prst3.dealer_code) dealer_code,max(prst3.dealer_name) dealer_name from picc_report_surveytwice prst3 where prst3.loss_item = '050' and prst3.twice_flag = '0' group by prst3.msg_id) prst2 on prm.id = prst2.msg_id left join dmsuserinfo d on pct.operator_user_code = d.usercode left join dmsuserinfo d1 on pct1.operator_user_code = d1.usercode left join picc_wx_report_main rm on prm.regist_id = rm.regist_id left join picc_report_msg_sz prms on prm.id = prms.msg_id left join picc_wx_report_no_upload rnu on prm.regist_id = rnu.regist_id where (pct.operator_user_code is not null or pct1.operator_user_code is not null)
最新发布
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值