zoukankan      html  css  js  c++  java
  • Codeforces Round #705 (Div. 2)C. K-beautiful Strings(贪心)

    https://codeforces.com/contest/1493/problem/C

    从后往左找一个需要加大的地方,枚举这个地方加大多少,每次都判定后面是否能凑成功即可

      1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
      2 #define bug(x) cout<<#x<<" is "<<x<<endl
      3 #include <bits/stdc++.h>
      4 #define iter ::iterator
      5 using namespace  std;
      6 typedef long long ll;
      7 typedef pair<int,ll>P;
      8 #define pb push_back
      9 #define mk make_pair
     10 #define se second
     11 #define fi first
     12 #define rs o*2+1
     13 #define ls o*2
     14 const ll mod=1e9+7;
     15 const int N=2e5+5;
     16 int T,n,k;
     17 
     18 char s[N],t[N];
     19 
     20 int a[N][26],b[26];
     21 
     22 int check(int i,int x){
     23     int f=0,y=0;
     24     for(int j=0;j<26;j++){
     25         if(j==x){
     26             b[j]=a[i-1][j];
     27             b[j]%=k;
     28             b[j]=k-b[j];
     29             b[j]--;
     30             if(b[j]<0){
     31                 f=1;
     32                 break;
     33             }
     34             b[j]%=k;
     35             y+=b[j];
     36             continue;
     37         }
     38         b[j]=a[i-1][j];
     39         b[j]%=k;
     40         b[j]=k-b[j];
     41         b[j]%=k;
     42         y+=b[j];
     43     }
     44     if(f)return -1;
     45     int z=n-i-y;
     46     if(z<0||z%k)return -1;
     47     return z;
     48 }
     49 
     50 
     51 int main(){
     52     //IO;
     53     cin>>T;
     54     while(T--){
     55         cin>>n>>k;
     56         scanf("%s",s+1);    
     57         for(int i=1;i<=n;i++){
     58             for(int j=0;j<26;j++)a[i][j]=0;
     59         }
     60         for(int i=1;i<=n;i++){
     61             for(int j=0;j<26;j++){
     62                 a[i][j]=a[i-1][j];
     63             }
     64             int x=s[i]-'a';
     65             a[i][x]++;
     66         }
     67         int f2=0;
     68         for(int i=0;i<26;i++){
     69             if(a[n][i]%k){
     70                 f2=1;
     71                 break;
     72             }
     73         }
     74         if(!f2){
     75             printf("%s
    ",s+1);
     76             continue;
     77         }
     78         int f=0;
     79         for(int i=n;i>=1;i--){
     80             if(s[i]=='z')continue;
     81             int x=s[i]-'a';
     82             int z=0,v=0;
     83             for(int p=x+1;p<26;p++){
     84                 z=check(i,p);
     85                 if(z!=-1){
     86                     v=p;
     87                     f=1;
     88                     break;
     89                 }          
     90             }
     91             if(!f)continue;
     92             for(int j=1;j<i;j++)t[j]=s[j];
     93             t[i]=v+'a';
     94             for(int j=i+1;j<=i+z;j++){
     95                 t[j]='a';
     96             }
     97             int h=i+z;
     98             for(int j=0;j<26;j++){
     99                 for(int w=1;w<=b[j];w++){
    100                     t[++h]=j+'a';
    101                 }
    102             }
    103             break;
    104         }
    105         if(f){
    106             t[n+1]='';
    107             printf("%s
    ",t+1);
    108         }
    109         else puts("-1"); 
    110     }
    111 
    112 }
  • 相关阅读:
    Maven学习--- 搭建多模块企业级项目
    Spring @Transactional ——事务回滚
    Spring事务异常回滚,捕获异常不抛出就不会回滚
    JVM调优总结(一)-- 堆和栈的基本概念
    Java中常见数据结构:list与map -底层如何实现
    hadoop 2.7.1安装和配置
    Hadoop基本开发环境搭建(原创,已实践)
    Hadoop下添加节点和删除节点
    iOS应用之间跳转
    iOS开发技巧
  • 原文地址:https://www.cnblogs.com/ccsu-kid/p/14503294.html
Copyright © 2011-2022 走看看