前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 1503 Advanced Fruits

HDU 1503 Advanced Fruits

作者头像
用户1624346
发布2018-04-17 16:16:09
5390
发布2018-04-17 16:16:09
举报
文章被收录于专栏:calmoundcalmound

题意:将两个单词合并,重复的部分只输出一次。

分析:最长递增子序列的变形,只是输出的地方发生了变化

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MN=200;
int len1,len2;
int b[MN][MN];
char s1[MN],s2[MN];
int c[MN][MN];
  
void LCSLenth()
{
    int i,j;
    memset(c,0,sizeof(c));
    for(i=1; i<=len1; i++)
    {
        for(j=1; j<=len2; j++)
        {
            if(s1[i-1]==s2[j-1])
            {
                c[i][j]=c[i-1][j-1]+1;
                b[i][j]=0;
            }
            else if(c[i][j-1]<=c[i-1][j])
            {
                c[i][j]=c[i-1][j];
                b[i][j]=1;//从上边
            }
            else
            {
                c[i][j]=c[i][j-1];
                b[i][j]=2;//从左边
            }
        }
    }
}
  
void Print(int i,int j)
{
    if(i==0 && j==0) return ;
    else if(i==0 && j!=0)
    {
        Print(i,j-1);
        printf("%c",s2[j-1]);
    }
    else if(i!=0 && j==0)
    {
        Print(i-1,j);
        printf("%c",s1[i-1]);
    }
    else if(b[i][j]==0)
    {
        Print(i-1,j-1);
        printf("%c",s1[i-1]);
    }
    else if(b[i][j]==1)
    {
        Print(i-1,j);
        printf("%c",s1[i-1]);
    }
    else
    {
        Print(i,j-1);
        printf("%c",s2[j-1]);//若从右边过来的,则打印s2
        //因为对于i是没发生变化的,也就是说s1序列位置没发生变化
    }
}
  
  
int main()
{
    int i,j;
    while(scanf("%s%s",&s1,&s2)!=EOF)
    {
        len1=strlen(s1);
        len2=strlen(s2);
        LCSLenth();
        Print(len1,len2);
        printf("\n");
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-03-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档