AYITACM2016省赛第三周 C-妥协(最长公共单词序列)
来源:互联网 发布:陕西网络广播 编辑:程序博客网 时间:2024/05/21 09:46
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 file 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 there will be at least one common word in each text. Each text 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
分析:
题意很简单,就是寻找两个文本文件的公共单词,动态规划的灵活运用,最重要的就是找到状态转移方程,多做题,了解动态规划的思想,就不会感觉那么难了!,再加上递归打印出结果,还是感觉灵活运用时最重要的!
#include<stdio.h>#include<string.h>int a[110][110],p[110][110],c,c1,f;char d1[110][35],d2[110][35],t;void LCS( ){ int i,j; memset(p,0,sizeof(p)); memset(a,0,sizeof(a)); for(i=1; i<c; i++) for(j=1; j<c1; j++) if(!strcmp(d1[i],d2[j])) //如果两个单词相等 { p[i][j]=p[i-1][j-1]+1;//公共序列长度加一 a[i][j]=1; } else if(p[i][j-1]<p[i-1][j]) //如果不相同,哪个序列长就等于哪个 { p[i][j]=p[i-1][j]; a[i][j]=2; } else { p[i][j]=p[i][j-1]; a[i][j]=3; }}void print(int i,int j){ if(a[i][j]==1)//如果等于1说明是公共单词 { print(i-1,j-1);//递归输出所有的结果 if(f) f=0; else printf(" "); printf("%s",d1[i]); } else if(a[i][j]==2) print(i-1,j); else if(a[i][j]==3)//寻找下一个公共单词 print(i,j-1);}int main(){ int i,j; while(scanf("%s",d1[1])!=EOF) { c=2,f=1,c1=1; while(scanf("%s",d1[c])&&d1[c][0]!='#') c++; while(scanf("%s",d2[c1])&&d2[c1][0]!='#') c1++; LCS();//找出最长公共子序列 print(c-1,c1-1);//输出最长公共子序列 printf("\n"); } return 0;}
- AYITACM2016省赛第三周 C-妥协(最长公共单词序列)
- AYITACM2016省赛第二周 H(最长公共子序列)
- AYITACM2016省赛第二周 G - History Grading历史评分(最长公共子序列)
- AYITACM2016省赛第三周 B-递增递减子序列(dp+二分)
- AYITACM2016省赛第三周A-Nested Dolls(贪心解嵌套问题)
- AYITACM2016省赛第三周D-max sum(DP最大字段和)
- AYITACM2016省赛第三周 J - Longest Run on a Snowboard(dp记忆化搜索)
- AYITACM2016省赛第三周 G - Unidirectional TSP(DP单向旅行商问题)
- AYITACM2016省赛第三周H - Let Me Count The Ways(递推)
- AYITACM2016省赛第三周F - Cutting Sticks(切木头,区间电dp合并)
- AYITACM2016省赛第三周 L - Anniversary party(树形dp)
- AYITACM2016省赛第三周M - Beijing Guards(贪心+二分)
- AYITACM2016省赛第三周I - Optimal Array Multiplication Sequence(dp)
- 第三章 最长公共子序列
- 求最长公共子序列(lcs.pas/c/cpp)
- 最长公共子序列(LCS) c语言/c++
- 最长公共子序列—C语言
- 20151215最长公共单词
- 对象的创建、内存布局、对象的访问定位
- vim 常用命令
- Python-OpenCV 处理图像(六)(七)(八):对象识别 图像灰度化处理 图像二值化处理
- MFC学习笔记(二)
- NSHelper.showAlertTitle的两种用法 swift
- AYITACM2016省赛第三周 C-妥协(最长公共单词序列)
- Android 笔记 SurfaceView实现涂鸦,轨迹重新绘制
- Xcode多种Build Configuration配置使用
- java学习笔记-io流浅析与实例
- curl 常用命令 post、get
- bzoj 1131: [POI2008]Sta(树形DP)
- Vs2015OpenCV静态编译出错
- RabbitMQ 安装、配置和用户管理(CentOS、SUSE)
- const