#include<bits/stdc++.h>
using namespace std;
char c[2007][2007];
char ans[4007];
int s[2007][2007];
int main(){
    memset(s,-1,sizeof(s));
    int n,k;
    scanf("%d%d",&n,&k);
    getchar();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%c",&c[i][j]);
        }
        getchar();
    }
    s[1][1]=k;//初始可改变的次数
    char mn=0;
    char tmp=0;
    for(int i=1;i<=n*2-1;i++){
        mn='z';
        for(int j=1;j<=i;j++){
            if(j>n||i-j+1>n)
                continue;
            if(s[j][i-j+1]>-1){//可达位置
                tmp=c[j][i-j+1];
                if(tmp!='a'&&s[j][i-j+1]>0)//改变机会未用完
                    tmp='a';
                mn=min(mn,tmp);
            }
        }
        ans[i]=mn;//i可走到的位置的最小值
        for(int j=1;j<=i;j++){
            if(j>n||i-j+1>n)
                continue;
            if(s[j][i-j+1]>-1){
                tmp=c[j][i-j+1];
                if(tmp!='a'&&s[j][i-j+1]>0){//贪心,前面有机会能变a就变
                    tmp='a';
                    s[j][i-j+1]--;
                }
                if(tmp==mn){//可行出路之一
                    if(j<n)
                        s[j+1][i-j+1]=max(s[j+1][i-j+1],s[j][i-j+1]);//走改变少的路
                    if(i-j+1<n)
                        s[j][i-j+2]=max(s[j][i-j+2],s[j][i-j+1]);//走改变少的路
                }
            }
        }
        printf("%c",ans[i]);
    }
    return 0;
}

Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)D(思维,DP,字符串)的更多相关文章

  1. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)

    Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...

  2. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path

    http://codeforces.com/contest/1072/problem/D bfs 走1步的最佳状态 -> 走2步的最佳状态 -> …… #include <bits/ ...

  3. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path(字典序)

    https://codeforces.com/contest/1072/problem/D 题意 给你一个n*n充满小写字母的矩阵,你可以更改任意k个格子的字符,然后输出字典序最小的从[1,1]到[n ...

  4. (AB)Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round

    A. Right-Left Cipher time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C. Connect Three 【模拟】

    传送门:http://codeforces.com/contest/1087/problem/C C. Connect Three time limit per test 1 second memor ...

  6. Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) C. Vasya and Golden Ticket 【。。。】

    任意门:http://codeforces.com/contest/1058/problem/C C. Vasya and Golden Ticket time limit per test 1 se ...

  7. Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) E. Vasya and Good Sequences(DP)

    题目链接:http://codeforces.com/contest/1058/problem/E 题意:给出 n 个数,对于一个选定的区间,区间内的数可以通过重新排列二进制数的位置得到一个新的数,问 ...

  8. Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)B. Personalized Cup

    题意:把一长串字符串 排成矩形形式  使得行最小  同时每行不能相差大于等于两个字符 每行也不能大于20个字符 思路: 因为使得行最小 直接行从小到大枚举即可   每行不能相差大于等于两个字符相当于  ...

  9. Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3) C. Playing Piano

    题意:给出一个数列 a1 a2......an  让你构造一个序列(该序列取值(1-5)) 如果a(i+1)>a(i) b(i+1)>b(i) 如果a(i+1)<a(i)  那么b( ...

随机推荐

  1. java:类集操作,多对多的关系

    java:类集操作,多对多的关系 //一个课程有多个学生报名, //一个学生可以报名多个课程 demo.java, Student.java, Course.java' public class Co ...

  2. Python基础-redis模块使用

    redis是一个数据库,他的数据全都是存放在内存里面的,redis每秒能支持30w次的读写,存放有两种格式,一种string类型,一种是hash类型 一,操作string类型 r=redis.Redi ...

  3. CodeForces Gym 100228 Graph of Inversions

    题目大意 对于一个长为$N$的序列$A$,定义它所对应的逆序图: 有$N$个点组成,标号为$1...N$的无向图,对于每一组$i,j(i<j)$若存在$A_i>A_j$则在新图中就存在一条 ...

  4. 记一次编译tensorflow-gpu爬过的坑

    废话不多说,先说最终成功的版本:系统=>centos7 ,cuda=>10.0 ,cudnn=>7.5 ,nccl=>源码编译, tensorflow=>最新版本源码编译 ...

  5. 图解Stm32使用jlink下载程序时jtag接口(SW和JTAG模式)的简化方法

    转自: http://www.it165.net/embed/html/201308/2332.html 用过stm32的人都知道stm32有两种常用下载程序的方法,用串口和jlink.串口下载方法和 ...

  6. springMVC绑定json参数之二(2.1)

    二.springmvc 接收不同格式的json字符串 1.首先扫盲几个知识点: 这个绑定json参数讲的都是用@RequestBody标签,所以前台必须传json字符串,不能是json对象,但是如果不 ...

  7. k8s组件简介

    Kubernetes is constructed using several components, as follows:f Kubernetes masterf Kubernetes nodes ...

  8. shell入门-sed-1

    sed这个工具比grep复杂一点,功能比grep复杂一点 功能也能指定匹配的行,不能颜色显示 sed 基础功能 [root@wangshaojun ~]# sed -n '10'p 1.txtuucp ...

  9. servlet课堂笔记

    1.servlet生命周期: 1> 加载和实例化 2> 初始化 init() 3> 处理请求 service()->doGet()/doPost() 4> 销毁 dest ...

  10. [matlab]一道笔试题

    x=[1 1; 1 -1; -1 -1; -1 1]'; X=-2:0.01:2; Y=X; N=length(X); [X,Y]=meshgrid(X,Y); Z1=0;Z2=0;Z3=0;Z4=0 ...