1. 程式人生 > >CodeForces 915C(DFS_E題)解題報告

CodeForces 915C(DFS_E題)解題報告

dfs play space 最大值 cstring 最大 sta cto 報告

題目鏈接:http://codeforces.com/problemset/problem/915/C

---------------------------------------------------------------------------------

題意:給你兩個數 a和 b,可以打亂 a每位數的順序,讓你求滿足 小於等於b 的最大值。。

思路:首先,觀察到數值比較大,所以采用字符串讀入的方式進行處理,通過比較字典序,交換不同位數是的保證每一次操作都是最優解。

代碼:

技術分享圖片
#include<cstdio>
#include<cstring>
#include<algorithm>
#include
<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll;
const double PI = acos(-1.0); const double eps = 1e-6; int numa =0; int numb =0; int main(void){ string a ; string b ; string sav; cin>>a>>b; numa = a.length(); numb =b.length(); //printf("%d %d\n",numa,numb); sort(a.begin(),a.end()); if(numa<numb){ reverse(a.begin(),a.end()); cout
<<a<<endl; return 0; } for(int i=0;i<numa;i++) { for(int j=numa-1;j>i;j--) { sav=a; swap(a[i],a[j]); sort(a.begin()+i+1,a.end()); if(a.compare(b)>0) a=sav; else break; } } cout<<a<<endl; return 0; }
View Code

CodeForces 915C(DFS_E題)解題報告