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网卡驱动安装及锐捷使用
原创博文,转载请注明出处 先吐槽一下,以前装了个Centos win7双系统, 然后手贱一不小心把启动文件给删了,接下来就用grub恢复启动文件,整了一天也没搞出来还把win7的Boot Manage ...
- [转]A Faster UIWebView Communication Mechanism
ref:http://blog.persistent.info/2013/10/a-faster-uiwebview-communication.html Use location.hash or t ...
- MKNetworkKit 使用
关于ios 网络请求之MKNetworkKit库的使用 项目导入MK库之后,还需要导入三个框架文件: SystemConfiguration.framework CFNetwork.framework ...
- 迷你MVVM框架 avalonjs 0.95发布
迷你MVVM框架 avalonjs 0.95发布 本版本最主要的改进是ms-with 深层绑定的实现,至少,avalon1.0所有重要的feature已经开发完毕,之后就是小补小漏,性能优化了. ms ...
- 冒泡动画按钮的简单实现(使用CSS3)
冒泡动画按钮的简单实现(使用CSS3) 原始的参考文章是 http://tutorialzine.com/2010/10/css3-animated-bubble-buttons/ ,基本原理是利用了 ...
- TOGAF架构开发方法(ADM)之迁移规划阶段
TOGAF架构开发方法(ADM)之迁移规划阶段 1.8 迁移规划(Migration Planning) 企业架构开发方法各阶段——迁移规划 1.8.1 目标 本阶段的目标是: 确保实施和迁移规划与企 ...
- j2ee面试宝典翻译(3) j2ee job interview companion
Q9:如何让表达“是一个”和“有一个”关系?或者请解释下“继承”和“组合”.组合和聚合之间有什么区别? A9:“是一个”的关系表示继承而“有一个”的关系是表示组合.继承和组合都允许你将子对象放入新类中 ...
- 你不知道的Eclipse用法:使用Allocation tracker跟踪Android应用内存分配
Android Tools中的DDMS带有一个很不错的跟踪内存分配的工具Allocation tracker.通过Alloction tracker,不仅知道分配了哪类对象,还可以知道在哪个线程.哪个 ...
- 一张图让你快速学会UML(聚合、组合、依赖、继承、接口、类)
有朋友反映,一上来直接讲设计模式就算理解了,也不知道如何画出类图,那么我们就通过一张图,来图解如何应用UML正确表示类与类之间的关系. 这张图完整讲述了鸟类的生存. 首先是类:在UML中,我们用分成三 ...
- php文件上传及头像预览
php文件上传原理是通过form表单的enctype="multipart/form-data"属性将文件临时放到wamp文件夹中的tmp目录下,再通过后台php 程序将文件保存在 ...