模拟。

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

#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. IL代码完结篇

    读懂IL代码就这么简单(三)完结篇   一 前言 写了两篇关于IL指令相关的文章,分别把值类型与引用类型在 堆与栈上的操作区别详细的写了一遍这第三篇也是最后一篇,之所以到第三篇就结束了,是因为以我现在 ...

  2. C++数据结构之二叉查找树(BST)

    C++数据结构之二叉查找树(BST) 二分查找法在算法家族大类中属于“分治法”,二分查找的过程比较简单,代码见我的另一篇日志,戳这里!因二分查找所涉及的有序表是一个向量,若有插入和删除结点的操作,则维 ...

  3. MKNetworkKit 使用

    关于ios 网络请求之MKNetworkKit库的使用 项目导入MK库之后,还需要导入三个框架文件: SystemConfiguration.framework CFNetwork.framework ...

  4. EasyUI项目驱动学习

    下面以一个项目简单介绍easyui的使用,主要包括以下组件 布局面板 - layout 可伸缩面板 - accordion 选项卡 - tabs 控制面板 - panel 窗口 - window 对话 ...

  5. 一位IT牛人的十年经验之谈

    1.分享第一条经验:“学历代表过去.能力代表现在.学习力代表未来.” 其实这是一个来自国外教育领域的一个研究结果.相信工作过几年.十几年的朋友对这个道理有些体会吧.但我相信这一点也很重要:“重要的道理 ...

  6. C#内获取泛型方法及扩展

    C#内获取泛型方法及扩展 C#内我们可以通过反射来动态获取实例方法和静态方法,不管方法存在多少个重载都可以通过GetMethod内的第二个参数去筛选出所要获取的方法,代码如下: type.GetMet ...

  7. android 工程里缺少 R.java 文件原因和解决方法

    作为新手,学习android 的时候难免要导入一些示例,目的为了更加了解android各种API用法,顺便也可以学习下别人代码的写法. 可是导入android源码后,基本都有错误,R.java也不会自 ...

  8. 使用ServletContextListener和HttpSessionListener两种监听器实现记录当前网站在线人数

    web.xml中配置: <listener>    <listener-class>com.mcm.listener.ServletContextListenerImpl< ...

  9. 最近学习了下BI(商业智能)做报表

    最近公司购买了Style intelligence 出的BI报表工具,接触新的东西不是很容易上手,这个东西是别的项目组用的,我们项目组由于进度比较快就让我先到他们项目组帮他们,为了使用这个东西,他们已 ...

  10. 百度地图在某架构下找不到符号.a文件的问题

    1.现象: 就是说找不到符号给i386的架构(就是模拟器).或者找不到符号给arm架构(真机). ld: warning: ignoring file /Users/pufang/xcode/demo ...