zoukankan      html  css  js  c++  java
  • poj 2250 Compromise

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

    题目大意:求两段文字的最长公共字串(有多组解时输出任意一种都行)

    解题思路:简单dp+搜索

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: poj 2250
      3 //user_id: SCNU20102200088
      4 ///////////////////////////////////////////////////////////////////////////
      5 
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <iterator>
      9 #include <iomanip>
     10 #include <cstring>
     11 #include <cstdlib>
     12 #include <string>
     13 #include <vector>
     14 #include <cstdio>
     15 #include <cctype>
     16 #include <cmath>
     17 #include <queue>
     18 #include <stack>
     19 #include <list>
     20 #include <set>
     21 #include <map>
     22 using namespace std;
     23 
     24 ///////////////////////////////////////////////////////////////////////////
     25 typedef long long LL;
     26 const double PI=acos(-1.0);
     27 ///////////////////////////////////////////////////////////////////////////
     28 
     29 ///////////////////////////////////////////////////////////////////////////
     30 //Add Code:
     31 string a[105],b[105],ans[105];
     32 int alen,blen,len,dp[105][105],num[105][105];
     33 
     34 void LCSLength(){
     35     memset(dp,0,sizeof(dp));
     36     memset(num,0,sizeof(num));
     37     for(int i=1;i<=alen;i++){
     38         for(int j=1;j<=blen;j++){
     39             if(a[i]==b[j]){
     40                 dp[i][j]=dp[i-1][j-1]+1;
     41                 num[i][j]=1;
     42             }
     43             else if(dp[i-1][j]>=dp[i][j-1]){
     44                 dp[i][j]=dp[i-1][j];
     45                 num[i][j]=2;
     46             }
     47             else{
     48                 dp[i][j]=dp[i][j-1];
     49                 num[i][j]=3;
     50             }
     51         }
     52     }
     53 }
     54 
     55 void LCS(int i,int j){
     56     if(i==0 || j==0) return ;
     57     if(num[i][j]==1){
     58         ans[len--]=a[i];
     59         LCS(i-1,j-1);
     60     }
     61     else if(num[i][j]==2) LCS(i-1,j);
     62     else LCS(i,j-1);
     63 }
     64 ///////////////////////////////////////////////////////////////////////////
     65 
     66 int main(){
     67     ///////////////////////////////////////////////////////////////////////
     68     //Add code:
     69     string s;
     70     while(cin>>s){
     71         alen=0,blen=0;
     72         a[++alen]=s;
     73         while(cin>>s){
     74             if(s[0]=='#') break;
     75             a[++alen]=s;
     76         }
     77         while(cin>>s){
     78             if(s[0]=='#') break;
     79             b[++blen]=s;
     80         }
     81         LCSLength();
     82         len=dp[alen][blen];
     83         LCS(alen,blen);
     84         cout<<ans[1];
     85         for(int i=2;i<=dp[alen][blen];i++) cout<<" "<<ans[i];
     86         cout<<endl;
     87     }
     88     ///////////////////////////////////////////////////////////////////////
     89     return 0;
     90 }
     91 
     92 ///////////////////////////////////////////////////////////////////////////
     93 /*
     94 Testcase:
     95 Input:
     96 die einkommen der landwirte
     97 sind fuer die abgeordneten ein buch mit sieben siegeln
     98 um dem abzuhelfen
     99 muessen dringend alle subventionsgesetze verbessert werden
    100 #
    101 die steuern auf vermoegen und einkommen
    102 sollten nach meinung der abgeordneten
    103 nachdruecklich erhoben werden
    104 dazu muessen die kontrollbefugnisse der finanzbehoerden
    105 dringend verbessert werden
    106 #
    107 Output:
    108 die einkommen der abgeordneten muessen dringend verbessert werden
    109 */
    110 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    mysql查看所有触发器以及存储过程等操作集合【转】
    Hutool之Http工具类使用
    SpringCloud之Sentinel
    SpringCloud之Gateway
    com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    [AWS DA Guru] SQS
    [AWS DA Guru] Kinesis
    [AWS DA Guru] SNS & SES
    [Typescript] Prevent Type Widening of Object Literals with TypeScript's const Assertions
    [AWS] Updating Elastic Beans Talks & RDS
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3269221.html
Copyright © 2011-2022 走看看