您的位置:首页 > 其它

UVa 10252 Common Permutation (water ver.)

2013-11-09 16:04 411 查看

10252-CommonPermutation

Timelimit:3.000seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1193

Giventwostringsoflowercaseletters,
a
andb,printthelongeststring
x
oflowercaseletterssuchthatthereisapermutationof
x
thatisasubsequenceofaandthereisapermutationof
xthatisasubsequenceofb.

Input

Inputfilecontainsseverallinesofinput.Consecutivetwolinesmakeasetofinput.Thatmeansintheinputfileline
1and2isasetofinput,line3and
4isasetofinputandsoon.Thefirstlineofapaircontains
aandthesecondcontainsb.Eachstringisonaseparatelineandconsistsofatmost
1000lowercaseletters.

Output

Foreachsetofinput,outputalinecontaining
x.Ifseveralxsatisfythecriteriaabove,choosethefirstoneinalphabeticalorder.

SampleInput:

pretty

women

walking

down

the

street


SampleOutput:

e

nw

et

水。

完整代码:

/*0.019s*/

#include<bits/stdc++.h>
usingnamespacestd;

chara[1005],b[1005],ans[1005];
boolvis[1005];

voidsolve(char*a,char*b)
{
intlena=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);
}

intmain()
{
while(gets(a))
{
gets(b);
if(strlen(a)<strlen(b))solve(a,b);
elsesolve(b,a);
}
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: