zoukankan      html  css  js  c++  java
  • Codeforces 1159D The minimal unique substring(构造)

    首先我们先观察三个串 1010,110110,11101110,答案都是红色部分,我们可以下一个结论,形如 abab ( a 中有非负整数个 1 , b 中只有一个 0 )这类的字符串答案恒为 2 ,也就是 k==2 ,然后就是用这类字符串去构造出我们所需的 k 。我们可以尝试从末尾加一个1,那么之前的串变成了 10101,1101101,111011101,那么答案为红色部分。我们可以发现,通过我们末尾添加的1,导致之前红色部分的 01 与我们末尾添加的1与前面一个0构成的 01 重复,使得之前的红色部分向后挪一位。于是,我们可以用这一规律去构造出我们想要的k,显然答案就是末尾部分的01(蓝色部分111...10111...10111满足 0 的个数加 1 的个数等于 k-1  ,那么对中间的影响(绿色部分111...1011111...110111)往后挪一位也就是我们的答案 k ,最后就是算出这形如 abab 字符串 a 部分中的 1 的个数有多少就行了,设 x 为 a 中 1 的个数,方程为 2*x+1+k-1=n  ,化简为 x=(n-k)/2 ,根据题意 n k 同奇偶,那么 x 也是唯一确定的,最后构造也由此生成。

     1 //      ——By DD_BOND
     2 
     3 //#include<bits/stdc++.h>
     4 #include<functional>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<sstream>
     8 #include<iomanip>
     9 #include<climits>
    10 #include<cstring>
    11 #include<cstdlib>
    12 #include<cstddef>
    13 #include<cstdio>
    14 #include<memory>
    15 #include<vector>
    16 #include<cctype>
    17 #include<string>
    18 #include<cmath>
    19 #include<queue>
    20 #include<deque>
    21 #include<ctime>
    22 #include<stack>
    23 #include<map>
    24 #include<set>
    25 
    26 #define fi first
    27 #define se second
    28 #define MP make_pair
    29 #define pb push_back
    30 #define INF 0x3f3f3f3f
    31 #define pi 3.1415926535898
    32 #define lowbit(a)  (a&(-a))
    33 #define lson l,(l+r)/2,rt<<1
    34 #define rson (l+r)/2+1,r,rt<<1|1
    35 #define Min(a,b,c)  min(a,min(b,c))
    36 #define Max(a,b,c)  max(a,max(b,c))
    37 #define debug(x)  cerr<<#x<<"="<<x<<"
    ";
    38 
    39 using namespace std;
    40 
    41 typedef long long ll;
    42 typedef pair<int,int> P;
    43 typedef pair<ll,ll> Pll;
    44 typedef unsigned long long ull;
    45 
    46 const ll LLMAX=2e18;
    47 const int MOD=1e9+7;
    48 const double eps=1e-8;
    49 const int MAXN=1e6+10;
    50 const int hmod1=0x48E2DCE7;
    51 const int hmod2=0x60000005;
    52 
    53 inline ll sqr(ll x){ return x*x; }
    54 inline int sqr(int x){ return x*x; }
    55 inline double sqr(double x){ return x*x; }
    56 ll __gcd(ll a,ll b){ return b==0? a: __gcd(b,a%b); }
    57 ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%MOD;a=a*a%MOD;n>>=1;}return sum;}
    58 inline int dcmp(double x){    if(fabs(x)<eps) return 0;    return (x>0? 1: -1); }
    59 
    60 int main(void)
    61 {
    62     ios::sync_with_stdio(false);    cin.tie(0);   cout.tie(0);
    63     int n,k;    cin>>n>>k;
    64     for(int i=0,j=(n-k)/2;i<n;i++){
    65         if(j)   cout<<1,j--;
    66         else cout<<0,j=(n-k)/2;
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    php操作redis cluster集群成功实例
    js算法-快速排序(Quicksort)
    如何编写自己的虚拟DOM
    一文带你看透kubernetes 容器编排系统
    SpringBoot AOP控制Redis自动缓存和更新
    机器学习中的numpy库
    时间戳转自定义日期格式和刚刚、十分钟前等友好日期格式
    Python数据处理实例
    Spark1.x和2.x如何读取和写入csv文件
    掌握这个Python小技巧,轻松构建cytoscape导入文件
  • 原文地址:https://www.cnblogs.com/dd-bond/p/10858347.html
Copyright © 2011-2022 走看看