zl程序教程

您现在的位置是:首页 >  其它

当前栏目

uva 10252 - Common Permutation 字符串水题

字符串 UVa Common 水题 Permutation
2023-09-27 14:22:35 时间

题意:給定兩個小寫的字串a與b,請印出皆出現在兩字串中的字母,出現的字母由a~z的順序印出,若同字母出現不只一次,請重複印出但不能超過任一字串中出現的次數。(from Ruby兔)

很水,直接比较输出即可。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1001;

int main() {
	char a[maxn], b[maxn];
	while (gets(a) != NULL && gets(b) != NULL) {
		sort (a, a + strlen(a));
		sort (b, b + strlen(b));
//		printf("%s %s\n", a, b);
		for (int i = 0, j = 0; i < strlen(a) && j < strlen(b);) {
			if (a[i] == b[j]) {
				printf("%c", a[i]);
				i++, j++;
			}
			else if (a[i] > b[j])
				j++;
			else
				i++;
		}//for
		printf("\n");
	}//while
	return 0;
}