对LCS进行储存

E - Advanced Fruits
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them. 
A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property. 

A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example. 

Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names. 

Input

Each line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters. 

Input is terminated by end of file. 

Output

For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable. 

Sample Input

apple peach
ananas banana
pear peach

Sample Output

appleach
bananas
pearch


这道题的对命名的规则本身就描述得不是很清楚,我都是看别人的代码才看懂的。代码大体思路都懂,但是有些细节地方却一知半解。不过感觉那个倒序存LCS的代码应该是个模板。


代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<stack>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

#define MaxSize 105
#define inf 0x3f3f3f3f
#define LL long long int

int n,m,cnt;
int dp[MaxSize][MaxSize];
char s1[MaxSize],s2[MaxSize];

struct node
{
  int i,j;
  char c;

} len[MaxSize];

void get_dp()//求出LCS长度
{
  n=strlen(s1+1);//注意s1和s2的开始地址,strlen是从给的地址一直扫到'\0',如果这里写成了strlen(s1),那么第一个就是'\0',n就是0了
  m=strlen(s2+1);

  for(int i=1; i<=n; i++)
    {
      for(int j=1; j<=m; j++)
        {
          if(s1[i]==s2[j])
            dp[i][j]=dp[i-1][j-1]+1;

          else
            dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
    }
}

void get_LCS()//储存LCS里面的字母
{
  int i=n;
  int j=m;
  cnt=1;

  while(i!=0 && j!=0)//倒着去找。这样能理解,没问题,但是不知道能不能用正序找。
    {
      if(s1[i]==s2[j] && dp[i][j] == dp[i-1][j-1]+1)
        {
          len[cnt].i=i;
          len[cnt].j=j;
          len[cnt].c=s1[i];

          cnt++;

          i--;
          j--;
        }

      else if(dp[i-1][j] > dp[i][j-1])//说明i-1之前有和s2[j]一样的字母,于是i就慢慢退,去找那个字母,那就是第一次让dp加1的公共字母(感觉上好像是这样,实际上并没有想明白)
        i--;

      else
        j--;
    }
}

int main()
{
  while(~scanf("%s%s",s1+1,s2+1))//从数组的1位置开始,会省去关于dp下标的不少麻烦
    {
        memset(dp,0,sizeof(dp));

        get_dp();

        get_LCS();

        int i=1;
        int j=1;
        for(int k=cnt-1;k>=1;k--)
        {
            while(i!=len[k].i)//将第一串的输出位置标记慢慢调整到LCS字母之前
            {
                printf("%c",s1[i]);

                i++;
            }
            while(j!=len[k].j)//将第二串的输出位置标记慢慢调整到LCS字母之前
            {
                printf("%c",s2[j]);

                j++;
            }

            printf("%c",len[k].c);//输出当前的LCS字母

            i++;
            j++;
        }

        for(int k=i;k<=n;k++)//把第一串LCS之后的字符串输出来
        printf("%c",s1[k]);

        for(int k=j;k<=m;k++)//把第二串LCS之后的字符串输出来
        printf("%c",s2[k]);

        printf("\n");
    }

  return 0;
}//FROM CJZ



资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值