programming-challenges Common Permutation (110303) 题解

蛮简单的,就是输入一定要用getline,用cin是过不了的。


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

int main() {
	string s1, s2; 

	while (getline(cin, s1) && getline(cin, s2)) {
		string ans;
		sort(s1.begin(), s1.end());
		sort(s2.begin(), s2.end());

		int i1 = 0, i2 = 0; 
		while (i1 < s1.size() && i2 < s2.size()) {
			if (s1[i1] == s2[i2]) {
				ans.push_back(s1[i1]);
				i1++; i2++;
			}
			else if (s1[i1] < s2[i2]) {
				i1++;
			}
			else {
				i2++;
			}
		}
		cout << ans << endl;
	}

	return 0;
}


你可能感兴趣的:(Algorithm,C++)