uva 10252

题意:給定兩個小寫的字串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
", 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("
");
	}//while
	return 0;
}





原文地址:https://www.cnblogs.com/java20130723/p/3212235.html