题目链接:https://codeforces.com/contest/670/problem/F

题意:

有一个非负整数 $n$,在它的右侧添上它的位数后,被发送出去;例如 $6510$,加上位数 $4$,变成 $65104$,发送出去。

但是,接受者接收到的数字则是被打乱了的,例如接收到了 $01465$,发送者只知道其中一段数字是什么,例如知道原数字中有一段是 $51$。

要你根据已知的信息推测出可能的 $n$ 中最小的那个。

题解:

首先在大约 $O(n)$ 的时间复杂度下可以知道这个数字是 $k$ 位。

然后,我们可以知道这个数字是由 $c_0$ 个 $0$、$c_1$ 个 $1$、……、$c_9$ 个 $9$,以及一段给定的一段数字 $t$ 组成的。

我们要考虑如何找出最小的那个,不妨考虑如下可能的情况:

1、$t$ + $c_0$ 个 $0$ + …… + $c_9$ 个 $9$。

2、在 $1 \sim 9$ 找一个最小的打头,剩下的依旧按照若干个 $0$ + …… + 若干个 $9$ 排列,其中 $t$ 按照 $t[0]$ 是什么,插在那个数的左侧或者右侧;例如 $10022599$,而 $t = [223]$,则有可能是答案的是 $100[223]22599$ 或者 $10022[223]599$;另一种例子是 $1089$,$t = [63]$,则可能是答案的是 $10[63]89$。

答案只可能是以上这些情况中的一种。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int SIZE=1e6+;
char s[SIZE],t[SIZE];
int slen,tlen;
int cnt_s[],cnt_t[];
int digit()
{
int dgt;
vector<int> v;
for(dgt=tlen;dgt<=slen;dgt++)
{
int tp[]={}, k=dgt;
while(k) ++tp[k%], k/=; bool ok=; for(int i=;i<;i++) if(cnt_t[i]+tp[i]>cnt_s[i]) ok=; int sum=;
for(int i=;i<;i++) sum+=cnt_s[i]-tp[i];
if(sum!=dgt) ok=; if(ok)
{
for(int i=;i<;i++) cnt_s[i]-=cnt_t[i]+tp[i];
return dgt;
}
}
}
bool check(const string& s) //检查前导零
{
if(s.size()> && s[]=='') return ;
else return ;
}
int main()
{
ios::sync_with_stdio();
cin.tie(), cout.tie(); cin>>s>>t;
slen=strlen(s), tlen=strlen(t); for(int i=;i<slen;i++) cnt_s[s[i]-'']++;
for(int i=;i<tlen;i++) cnt_t[t[i]-'']++;
int dgt=digit(); string res1,res2,res3; res1=t;
for(int i=;i<;i++)
{
for(int j=;j<=cnt_s[i];j++)
{
res1+=(char)(''+i);
}
} for(int i=;i<;i++)
{
if(cnt_s[i]>)
{
res2+=(char)(''+i);
res3+=(char)(''+i);
cnt_s[i]--;
break;
}
}
for(int i=;i<;i++)
{
if(t[]-''==i) res2+=(string)t; while(cnt_s[i]>)
{
res2+=(char)(''+i);
res3+=(char)(''+i);
cnt_s[i]--;
} if(t[]-''==i) res3+=(string)t;
} string ans;
if(!check(res1)) ans=(ans.size()?min(ans,res1):res1);
if(!check(res2)) ans=(ans.size()?min(ans,res2):res2);
if(!check(res3)) ans=(ans.size()?min(ans,res3):res3);
cout<<ans<<'\n';
}

Codeforces 670F - Restore a Number - [字符串]的更多相关文章

  1. CodeForces 670F Restore a Number

    模拟. 首先暴力找到答案的位数,然后就是分类讨论输出答案. #pragma comment(linker, "/STACK:1024000000,1024000000") #inc ...

  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 C. Split a Number(贪心大数运算)

    题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output ...

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

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

  6. Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)

    B. Split a Number time limit per test2 seconds memory limit per test512 megabytes inputstandard inpu ...

  7. Codeforces Round #300(A.【字符串,多方法】,B.【思维题】,C.【贪心,数学】)

    A. Cutting Banner time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

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

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

  9. 【codeforces 805D】Minimum number of steps

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

随机推荐

  1. xcode 编译或者打包的时候 找不到图片的错误

    进入app路径,copy一份图片进去就好了

  2. Axure RP for Mac(网站交互式原型设计工具)破解版安装

    1.软件简介    Axure RP 是 macOS 系统上一款最知名和最强大的原型设计工具,增加了大量新的特性,如应用多个动画,并同一时间运行一个小部件,如褪色,同时移动等,而且具有全新的图标和界面 ...

  3. Effective Java 第三版——49. 检查参数有效性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  4. 有关Linux下request.getRealPath("/")获取路径的问题

    request.getRealPath("/") 在window获取的是服务器的根目录,结尾包含分隔符, 如E:\apache-tomcat-6.0.29-bak\apache-t ...

  5. 分析各种Android设备屏幕分辨率与适配 - 使用大量真实安卓设备采集真实数据统计

    一. 数据采集 源码GitHub地址 : -- SSH : git@github.com:han1202012/DisplayTest.git; -- HTTP : https://github.co ...

  6. NodeJs相关系列文章

    1.图片上传之FileAPI与NodeJs 2.NodeJs之调试 3.CentOS下使用NVM 4.NodeJs之进程守护 5.Ubuntu下使用nvm 6.NodeJs之pm2 7.NodeJs之 ...

  7. 使用Python解析JSON详解

    为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. JSON 函数 ...

  8. Python进度条小实例

    代码理解: 函数view_bar(num,total) num是一个随即数,total是总数( num / total ) * 的int类型可以计算百分比 '\r%d%%%s' % (rate_num ...

  9. linux每日命令(37):top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...

  10. kafka项目中踩到的一个坑(客户端和服务器端版本不一致问题)

    启动项目时控制台抛出的异常信息: -- :: --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 't ...