UVA10252 POJ2629 Common Permutation【字符串排序】

509 篇文章 9 订阅
70 篇文章 2 订阅

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5782 Accepted: 1749

Description

Given two strings of lowercase letters, a and b, print the longest string x of lowercase letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.

Input

Input consists of pairs of lines. The first line of a pair contains a and the second contains b. Each string is on a separate line and consists of at most 1,000 lowercase letters.

Output

For each subsequent pair of input lines, output a line containing x. If several x satisfy the criteria above, choose the first one in alphabetical order.

Sample Input

pretty
women
walking
down
the
street

Sample Output

e
nw
et

Source



问题链接:UVA10252 POJ2629 Common Permutation

问题描述

  两个小写字母构成的字符串a和b,求各自的置换的最长公共子串,按字母顺序输出。

问题分析:(略)。

程序说明

  字符串类型变量的排序也是可以用函数sort()实现的。


AC的C++语言程序如下:

/* UVA10252 POJ2629 Common Permutation */

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>

using namespace std;

int main()
{
    string a, b;
    int lena, lenb;

    while(getline(cin, a) && getline(cin, b)) {
        // 计算字符串长度
        lena = a.size();
        lenb = b.size();

        // 字符串排序
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());

        // 匹配求公共子串,输出结果
        for(int i=0, j=0; i<lena && j<lenb; ) {
            if(a[i] == b[j]) {
                printf("%c", a[i]);
                i++, j++;
            } else if(a[i] > b[j])
                j++;
            else if(a[i] < b[j])
                i++;
        }
        printf("\n");
    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值