http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110303&format=html

UVA 10252 Common Permutation(公共排列)

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

题目大意:

输入:

输入包括若干组数据,每组数据均有连续的两行组成。第一行和第二行是一组,第三行和第四行一组,输入的每行都是由小写字母组成的字符串,

第一行代表a,第二行代表b。每个字符串的长度不超过1000

输出:

对于每组测试数据,在单独的一行输出x,如果有多个x满足条件,输出字典序最小的一个

//此题测试数据中字符串中应该有空格,用getline输入

样例输入:


pretty
women
walking
down
the
street
样例输出:
e
nw
et
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

#define N 105

int cmp(char a,char b)
{
return a<b;
}

int main()
{
int len1,len2,i,j;
string str1,str2;
while(getline(cin,str1),getline(cin,str2))
{
len1=str1.length();
len2=str2.length();;
sort(&str1[0],&str1[0]+len1,cmp); //先对两个字符串进行排序
sort(&str2[0],&str2[0]+len2,cmp); //保证按字典序排列
i=j=0;
//查找共有的字符,不同的忽略
while(i<len1&&j<len2)
{
if(str1[i]==str2[j])
{
cout<<str1[i];
i++;
j++;
}
else
{
if(str1[i]<str2[j])
i++;
else
j++;
}
}
cout<<endl;
}
return 0;
}

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

int cmp(char a,char b)
{
return a<b;
}

int main()
{
string str1,str2;
while(getline(cin,str1),getline(cin,str2))
{
int len1=str1.length();
int len2=str2.length();
int i,j,p=0;
sort(&str1[0],&str1[0]+len1,cmp);
sort(&str2[0],&str2[0]+len2,cmp);
for(i=0;i<len1;i++)
{
for(j=p;j<len2;j++)
{
if(str1[i]==str2[j])
{
cout<<str1[i];
p=j+1;
break;
}
}
}
cout<<endl;
}
return 0;
}


posted on 2012-03-02 10:04  pcoda  阅读(336)  评论(0编辑  收藏  举报