110303_Common Permutation(公共排列)
2012-03-02 10:04
253 查看
http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110303&format=htmlUVA 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; }
相关文章推荐
- UVa Problem 10252 Common Permutation (公共排列)
- 字符串 UVa 10252 Common Permutation (公共排列)
- UVa——110303 Common Permutation(字符串)
- programming-challenges Common Permutation (110303) 题解
- (排列生成算法)枚举排列的两种方法:递归枚举和next_permutation (使用多用next_permutation)
- poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)
- 公共排序-110303/10252
- Longest Common Prefix(最长公共前缀)
- JavaScript实现穷举排列(permutation)算法谜题解答
- POJ1330 Nearest Common Ancestors【最近公共祖先】【Tarjan-LCA算法】
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)
- lintcode :longest common substring 最长公共子串
- 寻找若干个字符串的最长公共前缀 Longest Common Prefix
- Git 管理多个分支的公共部分 How to manage the common part of multiple branches
- spoj1811 Longest Common Substring(LCS)最长公共子串
- hdu1423 Greatest Common Increasing Subsequence(最长递增公共子序列)
- LCA最近公共祖先(least common ancestors)
- [后缀自动机 模板题] SPOJ 1812 Longest Common Substring II & BZOJ 2946 [Poi2000]公共串
- hdu1403---Longest Common Substring(后缀数组求2个字符串的最长公共子串)