CodeForces 670F Restore a Number
模拟。
首先暴力找到答案的位数,然后就是分类讨论输出答案。
#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的更多相关文章
- Codeforces 670F - Restore a Number - [字符串]
题目链接:https://codeforces.com/contest/670/problem/F 题意: 有一个非负整数 $n$,在它的右侧添上它的位数后,被发送出去:例如 $6510$,加上位数 ...
- 【11.61%】【codeforces 670F】Restore a Number
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- Educational Codeforces Round 11 D. Number of Parallelograms 暴力
D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...
- Codeforces 980 E. The Number Games
\(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- 【codeforces 805D】Minimum number of steps
[题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...
随机推荐
- linux 编程技术
linux 编程技术No.1前期准备工作 GCC的编译过程分为预处理.生成汇编代码.生成目标代码和链接成可执行文件等4个步骤. 使用vim编写C 文件 : [lining@localhost prog ...
- 静态页面调试JS出现跨域问题
在chrome浏览器或者firefox浏览器里,由于安全限制的原因,本地调试JS,如果不配服务器环境而直接打开页面,那所有的AJAX操作会抛出下面错误: XMLHttpRequest cannot l ...
- MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 六:支持多数据库操作
本次主要内容:修正MongoDB.Repository框架对多数据库的支持. 在之前的五篇文章中对MongoDB.Repository框架做了简单的介绍是实现思路.之前是考虑MongoDB.Repos ...
- 利用pdf2swf将PDF转换成SWF
将PDF转换成SWF可以使用SWFTools工具中的pdf2swf(http://www.swftools.org/),CSDN快速免积分下载地址http://download.csdn.net/de ...
- 在多线程中进行UI操作--ios学习笔记
iOS 上不建议在非主线程进行UI操作,在非主线程进行UI操作有很大几率会导致程序崩溃,或者出现预期之外的效果. 我开始不知道这一点,在子线程中进行了弹窗操作,结果程序就出问题了! 报的错误是(EXC ...
- 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 ...
- rabbitmq在mac上安装
1.安装brew 打开http://bash.sh 执行 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ ...
- enode框架step by step之Staged event-driven architecture思想的运用
enode框架step by step之Staged event-driven architecture思想的运用 enode框架系列step by step文章系列索引: 分享一个基于DDD以及事件 ...
- cooking eggs
1: what is egg? what's the shape of it in details? 2: can egg run like this http://item.taobao.com/i ...
- Java Concurrency (1)
Memory that can be shared betweenthreads is called shared memory or heap memory. The term variable a ...