您的位置:首页 > 其它

UVA10252 POJ2629 Common Permutation【字符串排序】

2017-03-17 08:34 399 查看
Common permutation

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
The UofA Local 1999.10.16

问题链接: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: