UVA 10252 (13.07.13)

本文介绍了一个算法问题:寻找两个字符串中最长的公共排列,并提供了一段AC代码作为解决方案。该问题涉及读取标准输入中的两个小写字母字符串,输出它们共有的最长子序列,考虑到了字符重复的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Common Permutation

Input: standard input

Output: standard output

Time Limit: 4 seconds

Memory Limit: 32 MB

 

Giventwo strings of lowercase letters, aand b, print the longeststring x of lowercase letterssuch that there is a permutation of xthat is a subsequence of aand there is a permutation ofxthat is a subsequence ofb.

 

Input

Input file contains several lines of input.Consecutive two lines make a set of input. That means in the input file line1 and2 is a set of input, line3and4 is a set of input and so on.The first line of a pair containsaand the second containsb.Each string is on a separate line and consists of at most1000 lowercase letters.

 

Output

For each set of input, output a line containingx. If severalx satisfy the criteria above,choose the first one in alphabetical order.

 

Sample Input:

pretty
women
walking
down
the
street
 

Sample Output:

e

nw

et


这题开始WA了一次

后来对照了一份其他AC的代码

发现是没考虑到重复出现的情况

例如

abccccddd

bcccdd

输出是bcd

而是bcccdd


AC代码如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main () {
	char a[1005], b[1005];
	while(gets(a) && gets(b)) {
		int mark1[26] = {0};
		int mark2[26] = {0};
		int mark3[26] = {0};
		char mark[27] = {"abcdefghijklmnopqrstuvwxyz"};
		memset(mark2, 0, sizeof(mark2));
		int i, j;
		int len1 = strlen(a);
		int len2 = strlen(b);
		for(i = 0; i < len1; i++)
			mark1[a[i] - 'a'] ++;
		for(i = 0; i < len2; i++)
			mark2[b[i] - 'a'] ++;
		for(i = 0; i < 26; i++) {
			if(mark1[i] == 0 || mark2[i] == 0)
				mark3[i] = 0;
			else {
				if(mark1[i] > mark2[i])
					mark3[i] = mark2[i];
				else
					mark3[i] = mark1[i];
			}
		}
		for(i = 0; i < 26; i++) {
			if(mark3[i] != 0) {
				for(j = 0; j < mark3[i]; j++)
					printf("%c", mark[i]);
			}
		}
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值