您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: