论bzoj的水题们、、、
这题的话,直接枚举目标串的前缀即可
然后往后加字母直接贪心就可以啦~
1 /************************************************************** 2 Problem: 3799 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:88 ms 7 Memory:820 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cstring> 12 #include <algorithm> 13 14 using namespace std; 15 const int N = 5005; 16 17 int n, m; 18 int a[128], b[128]; 19 char s[N], t[N], ans[N]; 20 21 int main() { 22 int i, j; 23 bool flag; 24 gets(s + 1), gets(t + 1); 25 n = strlen(s + 1), m = strlen(t + 1); 26 for (i = 1; i <= n; ++i) 27 ++a[s[i]]; 28 for (i = m + 1; i; --i) { 29 for (j = 0; j < 128; ++j) b[j] = a[j]; 30 flag = 0; 31 for (j = 1; j < i; ++j) { 32 ans[j] = t[j]; 33 if (--b[t[j]] < 0) flag = 1; 34 } 35 if (flag) continue; 36 for (j = t[i] + 1; j < 128; ++j) 37 if (b[j]) { 38 flag = 1; 39 break; 40 } 41 if (!flag) continue; 42 ans[i] = j, --b[j]; 43 for (j = 1; j <= i; ++j) 44 putchar(ans[j]); 45 for (j = 0; j < 128; ++j) 46 while (b[j]--) putchar(j); 47 putchar('\n'); 48 return 0; 49 } 50 puts("-1"); 51 return 0; 52 }
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen