您的位置:首页 > 其它

Educational Codeforces Round 36 (Rated for Div. 2) C 不大于某个数的序列变换

2018-01-19 12:42 387 查看
C. Permute Digitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.It is allowed to leave a as it is.InputThe first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.OutputPrint the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.Examplesinput
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output
4940
给出A,B。对A进行变换,变为不大于B的最大的那个数。
这道题就是逐个暴力,从最大的开始找,如果找到,立即break。之后在查找下一位。
例如123 222
首先123对第一位进行处理,和最大的3呼唤,变为321,后两位排序 312. 比222大,不行
在和2互换,213,后两位排序213,可以。
之后对第二位进行处理,12呼唤,231,比222大,不行。
所以最终就是213.
就是对每一个位置进行暴。
#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
   string a,b;
   cin>>a>>b;
   int len1=a.length();
   int len2=b.length();
   sort(a.begin(),a.end());
   if(len1<len2)
   {
       for(int i=len1-1;i>=0;i--)
       {
           cout<<a[i];
       }
       cout<<endl;
       return 0;
   }
   for(int i=0;i<len1;i++)
   {
       for(int j=len1-1;j>i;j--)
       {
           string c=a;
           swap(a[i],a[j]);
           sort(a.begin()+i+1,a.end());
           if(a>b)
           {
               a=c;
           }
           else
           {
               break;
           }
       }
   }
   cout<<a<<endl;

}#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
   string a,b;
   cin>>a>>b;
   int len1=a.length();
   int len2=b.length();
   sort(a.begin(),a.end());
   if(len1<len2)
   {
       for(int i=len1-1;i>=0;i--)
       {
           cout<<a[i];
       }
       cout<<endl;
       return 0;
   }
   for(int i=0;i<len1;i++)
   {
       for(int j=len1-1;j>i;j--)
       {
           string c=a;
           swap(a[i],a[j]);
           sort(a.begin()+i+1,a.end());
           if(a>b)
           {
               a=c;
           }
           else
           {
               break;
           }
       }
   }
   cout<<a<<endl;
   
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: