模拟。

首先暴力找到答案的位数,然后就是分类讨论输出答案。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
char s[maxn],t[maxn];
int f[],g[],len,tLen,sLen; bool check(int x)
{
int tmp[],sum=,tt=x;
for(int i=;i<;i++) tmp[i]=f[i];
while(x)
{
tmp[x%]--;
if(tmp[x%]<) return ;
x=x/; sum++;
} if(tt+sum==sLen)
{
bool flag=; for(int i=;i<;i++) if(tmp[i]) flag=;
if(flag) return ;
else
{
if(t[]!='') return ;
return ;
}
}
return ;
} char num[][maxn];
char tmp[maxn],ans[maxn];
char tmp1[maxn],tmp2[maxn]; int main()
{
scanf("%s%s",s,t); sLen=strlen(s); tLen=strlen(t); for(int i=;s[i];i++) f[s[i]-'']++;
for(int i=;t[i];i++) g[t[i]-'']++; len=tLen; if(t[]=='') len++; for(int i=;i<;i++) f[i]=f[i]-g[i]; bool F=;
for(;len<=;len++) { if(check(len)) {F=;break;} } if(F==) { printf("0\n"); return ;}
int ttt=len; while(ttt) f[ttt%]--,ttt=ttt/; if(t[]=='')
{
int xx; for(int i=;i<;i++) {if(f[i]){ xx=i;break; }} f[xx]--; printf("%d",xx); int sz=;
for(int i=; i<; i++)
{
if(f[i]==) continue;
for(int j=; j<f[i]; j++) num[sz][j]=i+'';
sz++;
} bool ff=;
for(int i=;i<sz;i++)
{
if(ff==) printf("%s",num[i]);
else
{
char h1[maxn],h2[maxn]; h1[]=h2[]=; strcpy(h1,num[i]); strcat(h1,t);
strcpy(h2,t); strcat(h2,num[i]); if(num[i][]<t[]) printf("%s",num[i]);
else if(strcmp(h1,h2)<) printf("%s",num[i]);
else { printf("%s%s",t,num[i]); ff=; }
}
}
if(ff==) printf("%s",t);
printf("\n");
} else
{ int xx=-; for(int i=;i<;i++) {if(f[i]){ xx=i;break; }}
if(xx==-)
{
printf("%s",t);
for(int i=;i<f[];i++) printf(""); printf("\n");
} else if(xx>t[]-'')
{
printf("%s",t);
for(int j=;j<;j++)
for(int i=;i<f[j];i++) printf("%d",j);
printf("\n");
} else if(xx==t[]-'')
{
if(f[]==)
{
for(int i=;i<f[xx];i++) tmp[i]=xx+''; tmp[f[xx]]=; char h1[maxn],h2[maxn]; h1[]=h2[]=; strcpy(h1,t); strcat(h1,tmp);
strcpy(h2,tmp); strcat(h2,t); if(strcmp(h1,h2)<) strcpy(ans,h1);
else strcpy(ans,h2); int tt=strlen(ans);
for(int i=xx+;i<;i++)
for(int j=;j<f[i];j++)
ans[tt++]=i+''; ans[tt]=;
printf("%s\n",ans);
}
else
{
tmp1[]=tmp2[]=;
strcpy(tmp1,t); int tt=strlen(tmp1);
for(int i=;i<f[];i++) tmp1[tt++]=+''; for(int i=xx;i<;i++)
for(int j=;j<f[i];j++)
tmp1[tt++]=(i+'');
tmp1[tt]=; tmp2[]=xx+''; f[xx]--; tt=;
for(int i=;i<f[];i++) tmp2[tt++]=(+''); tmp2[tt]=; for(int i=;i<f[xx];i++) tmp[i]=xx+''; tmp[f[xx]]=; char h1[maxn],h2[maxn]; h1[]=h2[]=; strcpy(h1,t); strcat(h1,tmp);
strcpy(h2,tmp); strcat(h2,t); if(strcmp(h1,h2)<) strcat(tmp2,h1);
else strcat(tmp2,h2);
tt=strlen(tmp2); for(int i=xx+;i<;i++)
for(int j=;j<f[i];j++)
tmp2[tt++]=i+''; tmp2[tt]=; if(strcmp(tmp1,tmp2)>) printf("%s\n",tmp2);
else printf("%s\n",tmp1);
}
} else
{
f[xx]--; printf("%d",xx); int sz=;
for(int i=; i<; i++)
{
if(f[i]==) continue; for(int j=; j<f[i]; j++) num[sz][j]=i+''; num[sz][f[i]]=;
sz++;
} bool ff=;
for(int i=;i<sz;i++)
{
if(ff==) printf("%s",num[i]);
else
{
char h1[maxn],h2[maxn]; h1[]=h2[]=; strcpy(h1,num[i]); strcat(h1,t);
strcpy(h2,t); strcat(h2,num[i]); if(num[i][]<t[]) printf("%s",num[i]);
else if(strcmp(h1,h2)<) printf("%s",num[i]);
else { printf("%s%s",t,num[i]); ff=; }
}
}
if(ff==) printf("%s",t);
printf("\n");
}
}
return ;
}

CodeForces 670F Restore a Number的更多相关文章

  1. Codeforces 670F - Restore a Number - [字符串]

    题目链接:https://codeforces.com/contest/670/problem/F 题意: 有一个非负整数 $n$,在它的右侧添上它的位数后,被发送出去:例如 $6510$,加上位数 ...

  2. 【11.61%】【codeforces 670F】Restore a Number

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

    F. Restore a Number   Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...

  4. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

  5. dp --- Codeforces 245H :Queries for Number of Palindromes

    Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H M ...

  6. Educational Codeforces Round 11 D. Number of Parallelograms 暴力

    D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...

  7. Codeforces 980 E. The Number Games

    \(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...

  8. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  9. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

随机推荐

  1. WPF专业编程指南 - 那些根元素<>的解释

    <Window x:Class="Chapter1_WpfApplication1.MainWindow" xmlns="http://schemas.micros ...

  2. 简单的mvc之三:灵活的路由(上)

    在上一篇关于管线的随笔中已经提及了管线,通过对管线的分析,我们可以得到下面几个结论:路由系统由URLRoutingModule模块实现,它订阅了PostResolvRequestCache事件:路由系 ...

  3. CKEditor4.x部署和配置

    CKEditor4.x && JSP 官网下载CKEditor,可选Basic, Standard, Full 解压放置其WebRoot下 JSP中引入以下文件: <script ...

  4. CYQ.Data 支持WPF相关的数据控件绑定.Net获取iis版本

    CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09) 事件的结果 经过多天的思考及忙碌的开发及测试,CYQ.Data 终于在UI上全面支持WPF,至此,CYQ.Data 已经可以方便 ...

  5. JS实现以日历形式显示当前时间

    效果图: <script language="Javascript"> var datelocalweek=new Array("星期日", &qu ...

  6. HC - 05 bluetooth module settings in Linux using CuteCom

    By default the bluetooth module HC-05 sets baud rate at 38400, data bits 8, Stop bits 1 All schemati ...

  7. hdu4284之字典树

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. Python 3语法小记(五)字符串

    Python 3 的源码的默认编码方式为 UTF-8 在Python 3,所有的字符串都是使用Unicode编码的字符序列. utf-8 是一种将字符编码成字节序列的方式.字节即字节,并非字符.字符在 ...

  9. Android Phone和Pad UA区别

    很多Android开发者或者网站端都可能会困扰关于如何区分Android phone和Android Pad的ua.确实这个问题很困难,我也曾被困扰了一段时间,后来在Stackoverflow中发现了 ...

  10. snmpwalk高延时问题分析

    问题出现 有两台物理机,一台是192.168.1.15,另一台是192.168.1.43.二者的netsnmp版本相同. 使用snmpwalk去访问两台机器,获取tcp重传数(tcpRetransSe ...