UVa Problem Solution: 10252 - Common Permutation
2008-11-13 15:48
645 查看
Really simple problem. However, two points need to be considered:
1. ... find the largest string x such that there is a permutation of x that is a (not necessarily continuous) subsequence of a ...
2. if there are spaces in the input, ignore them in the output.
Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 10252 C++ "Common Permutation" */
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const linesize = 1001;
int const ncharacters = 128;
void common_permutation(char *line1, char *line2, char common[])
{
int count1[ncharacters] = {0};
int count2[ncharacters] = {0};
for (int i = 0; line1[i] != '/0'; ++i) {
++count1[line1[i]];
}
for (int i = 0; line2[i] != '/0'; ++i) {
++count2[line2[i]];
}
int c = 0;
for (int i = 0; i < ncharacters; ++i) {
if (char(i) == ' ') continue;
int min = count1[i] < count2[i] ? count1[i] : count2[i];
for (int j = 0; j < min; ++j) {
common[c++] = char(i);
}
}
common[c] = '/0';
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
char line1[linesize];
char line2[linesize];
char common[linesize];
while (cin.getline(line1, linesize).getline(line2, linesize)) {
common_permutation(line1, line2, common);
cout << common << '/n';
}
return 0;
}
1. ... find the largest string x such that there is a permutation of x that is a (not necessarily continuous) subsequence of a ...
2. if there are spaces in the input, ignore them in the output.
Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 10252 C++ "Common Permutation" */
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const linesize = 1001;
int const ncharacters = 128;
void common_permutation(char *line1, char *line2, char common[])
{
int count1[ncharacters] = {0};
int count2[ncharacters] = {0};
for (int i = 0; line1[i] != '/0'; ++i) {
++count1[line1[i]];
}
for (int i = 0; line2[i] != '/0'; ++i) {
++count2[line2[i]];
}
int c = 0;
for (int i = 0; i < ncharacters; ++i) {
if (char(i) == ' ') continue;
int min = count1[i] < count2[i] ? count1[i] : count2[i];
for (int j = 0; j < min; ++j) {
common[c++] = char(i);
}
}
common[c] = '/0';
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
char line1[linesize];
char line2[linesize];
char common[linesize];
while (cin.getline(line1, linesize).getline(line2, linesize)) {
common_permutation(line1, line2, common);
cout << common << '/n';
}
return 0;
}
相关文章推荐
- UVa Problem 10252 Common Permutation (公共排列)
- uva 10252 - Common Permutation 字符串水题
- UVa 10252 Common Permutation (water ver.)
- uva 10252 - Common Permutation 字符串水题
- UVa 10252 - Common Permutation
- (字符串的处理4.7.14)UVA 10252 Common Permutation(寻找两个字符串中的共同字符&&按字典序输出)
- Uva 10252 - Common Permutation
- 字符串 UVa 10252 Common Permutation (公共排列)
- uva 10252Common Permutation
- uva-10252-Common Permutation
- UVA--10252 Common Permutation
- UVa10252:Common Permutation
- UVa Problem Solution: 10033 - Interpreter
- UVa Problem Solution: 10050 - Hartals
- UVa Problem Solution: 10258 - Contest Scoreboard
- UVa Problem Solution: 120 - Stacks of Flapjacks
- UVa Problem Solution: 701 - The Archaeologist's Dilemma
- UVa Problem Solution: 10213 - How Many Pieces of Land?
- UVa Problem Solution: 846 - Steps
- UVa Problem Solution: 10110 - Light, More Light