10252-common permutation
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1193
Given two strings of lowercase letters, a and B, print the longest string x of lowercase letters such that's there is a perm Utation of X that's a subsequence of a and there is a permutation OFX this is a subsequence of B.
Input
Input file contains several lines of input. Consecutive two lines make a set of input. That means in the input file line1 and 2 are a set of input, line 3 And4 are a set of input and so on. The A pair Containsa and the second contains B. Each of the separate line and consists of an at most1000 lowercase letters.
Output
For each set of input, output a line containingx. If several x satisfy the criteria above, choose the "one in alphabetical" order.
Sample Input:
Pretty
Women
Walking
Down
The
Street
Sample Output:
E
nw
Et
Water.
Complete code:
/*0.019s*/
#include <bits/stdc++.h>
using namespace std;
Char a[1005], b[1005], ans[1005];
BOOL vis[1005];
void Solve (char* A, char* b)
{
int lena = strlen (a), LenB = strlen (b), I, j, c = 0;
memset (Vis, 0, sizeof (VIS));
for (i = 0; i < Lena. ++i) for
(j = 0; j < LenB; ++j)
{
if (!vis[j] && a[i] = = B[j])
{
ans[c++] = A[i];
VIS[J] = true;
break;
}
}
Ans[c] = 0;
Sort (ans, ans + c);
Puts (ans);
int main ()
{while
(gets (a))
{
gets (b);
if (strlen (a) < strlen (b)) solve (A, b);
else Solve (b, a);
}
return 0;
}
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45441.htm