题目链接: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. 【转载】JAVA基础:注解

    原文:https://www.cnblogs.com/xdp-gacl/p/3622275.html#undefined 一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的 ...

  2. logback配置详解

    本文转自:https://segmentfault.com/a/1190000008315137 概览 简单地说,Logback 是一个 Java 领域的日志框架.它被认为是 Log4J 的继承人.L ...

  3. [svc]inotify+rsync解决nfs单点问题

    安装配置inotify 参考 yum install inotify* -y [root@n2 shell]# rpm -qa|grep inotify inotify-tools-3.14-8.el ...

  4. 【Linux】Linux基本命令扫盲

    [VI使用] 1.在命令行模式     :在vi编辑器中将光标放在函数上, 定位到每行的开头,不进入输入模式,$ 定位到每行的末尾     :[n]dd 剪贴n行,p粘贴     : dG 删除光标后 ...

  5. Java编程的逻辑 (83) - 并发总结

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  6. SASS常用语法

    原文地址:这里 @charset "UTF-8"; /** * 自定义变量 */ $blue: #1875e7; div { color: $blue; } /** * 变量要嵌在 ...

  7. Java8学习笔记(二)--三个预定义函数接口

    三个函数接口概述 JDK预定义了很多函数接口以避免用户重复定义.最典型的是Function: @FunctionalInterface public interface Function<T, ...

  8. [UFLDL] Linear Regression & Classification

    博客内容取材于:http://www.cnblogs.com/tornadomeet/archive/2012/06/24/2560261.html Deep learning:六(regulariz ...

  9. SparkSQL demo

    1.数据样本:data1.txt xiaoming,25,chengduxiaohua,23,beijingliuyang,16,hangzhouxiaoqiang,19,zhejiang 2.dem ...

  10. /usr/bin/ld: cannot find -lncurses是咋回事?

    你的系統是32位的還是64位的? 如果是32位的就用:sudo apt-get install libncurses5-dev 如果是64位的,就用:sudo apt-get install lib3 ...