模拟。

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

#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. discuz的门户文章页中增加百度分享代码

    discuz虽然有百度分享插件,但是不太想用,于是自己手动添加了百度分享代码: 一.在http://share.baidu.com/地址中申请设置自己的百度分享代码,选择的风格完全按照个人喜好进行选择 ...

  2. CC/G++ 学习笔记

    CC/G++ 学习笔记 本文是<An introduction to GCC>的学习笔记,记录使用GCC/G++主要的实用技巧,本文讲述的知识基本上摘自本书,附带自己的一些体验.如果想详细 ...

  3. [置顶] 实现360度全景图像的利器--PanoramaGL

    传送门 ☞轮子的专栏 ☞转载请注明 ☞ http://blog.csdn.net/leverage_1229 介绍 本指南将介绍一个PanoramaGL 0.1类库的简单用法,更多的细节请签出 Hel ...

  4. CSS sprites减少HTTP请求

    使用CSS sprites减少HTTP请求   sprites是鬼怪,小妖精,调皮鬼的意思,初听这个高端洋气的名字我被震慑住了,一步步掀开其面纱后发觉很简单的东西,作用却很大 神马是CSS 小妖精 C ...

  5. 64位下Hook NtOpenProcess的实现进程保护 + 源码 (升级篇 )

    64位下Hook NtOpenProcess的实现进程保护 + 源码 (升级篇 ) [PS: 如果在64位系统下,出现调用测试demo,返回false的情况下,请修改Hook Dll的代码] glhH ...

  6. js框架漫谈

    现在实际项目中可供选择的javascript框架很多,热门的有jquery,dojo,mootools,ext等.这些框架按照不同的标准有不同的分类方法,比如按照扩展方式便可分为prototype式的 ...

  7. data格式加载图片

    html img 使用data格式加载图片   背景 这久闲来无事给一位客户测试一款体检软件,是winform结构的,其中有一个功能是需要把生成的体检报告导出为html格式.测试导出后直接在谷歌浏览器 ...

  8. 判断0-N之间出现1的次数

    Console.WriteLine("请输入截止数字?退出请输入y"); string input = Console.ReadLine(); int n = Convert.To ...

  9. Centos 64位 Install certificate on apache 即走https协议

    Centos 64位 Install certificate on apache 即走https协议 一: 先要apache 请求ssl证书的csr 一下是步骤: 重要注意事项 An Importan ...

  10. Arduino 不同Arduino衍生板子的问题

    arduino IDE装上的时候,要记得在windows平台安装驱动. 如果不安装驱动的话,烧写程序的时候也许会遇到下面的现象. 原因有如下几种: 1,arduino控制板或者COM口没有选对,这种问 ...