UVa Problem Solution: 10252 - Common Permutation


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:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10252 C++ "Common Permutation" */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <vector>
  10. using namespace std;
  11.      
  12. int const linesize = 1001;
  13. int const ncharacters = 128;
  14. void common_permutation(char *line1, char *line2, char common[])
  15. {
  16.   int count1[ncharacters] = {0};
  17.   int count2[ncharacters] = {0};
  18.   for (int i = 0; line1[i] != '/0'; ++i) {
  19.     ++count1[line1[i]];
  20.   }
  21.   for (int i = 0; line2[i] != '/0'; ++i) {
  22.     ++count2[line2[i]];
  23.   }
  24.   int c = 0;
  25.   for (int i = 0; i < ncharacters; ++i) {
  26.     if (char(i) == ' 'continue;
  27.     int min = count1[i] < count2[i] ? count1[i] : count2[i];
  28.     for (int j = 0; j < min; ++j) {
  29.       common[c++] = char(i);
  30.     }
  31.   }
  32.   common[c] = '/0';
  33. }
  34.           
  35. int main(int argc, char *argv[])
  36. {
  37. #ifndef ONLINE_JUDGE
  38.   filebuf in, out;
  39.   cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
  40.   cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
  41. #endif
  42.   char line1[linesize];
  43.   char line2[linesize];
  44.   char common[linesize];
  45.   while (cin.getline(line1, linesize).getline(line2, linesize)) {
  46.     common_permutation(line1, line2, common);
  47.     cout << common << '/n';
  48.   }
  49.   return 0;
  50. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值