#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. Linux课程---8、Linux启动流程

    Linux课程---8.Linux启动流程 一.总结 一句话总结: grub启动引导界面(比如装了多系统,选择系统),找到linux内核 启动init程序,加载各种驱动 1.进入grub界面选择相应的 ...

  2. ES doc_values的来源,field data——就是doc->terms的正向索引啊,不过它是在查询阶段通过读取倒排索引loading segments放在内存而得到的?

    Support in the Wild: My Biggest Elasticsearch Problem at Scale Java Heap Pressure Elasticsearch has ...

  3. Python习题-输出一个字符串中最长的子字符串及其长度

    描述:有个字符串$sd1#111$svda123!!!221&eSSDSDG,包含特殊字符.数字和字母,输出最长的子字符串和他的长度#例如上面的字符串包含数字字母的字符串是svda123,长度 ...

  4. hdu-5641 King's Phone (水题)

    题目链接: King's Phone Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

  5. 机器学习 Support Vector Machines 1

    引言 这一讲及接下来的几讲,我们要介绍supervised learning 算法中最好的算法之一:Support Vector Machines (SVM,支持向量机).为了介绍支持向量机,我们先讨 ...

  6. noip模拟赛Bywzj52501 17.10.18

    T1 rob 环形消灭虫子 先想出了一个n^2暴力 然后我们想到 如果从两个连续的点求解 则会出现仅有的两种结果 (因为这两种情况的交是全集) 当时因为Naive求了50次 #include<i ...

  7. jQuery常用ajax操作

    在做asp.net项目的时候经常会用到ajax操作,现总结常用的ajax操作供平时项目中参考 第一种: 前端代码: <script type="text/javascript" ...

  8. word2010以上版本中快捷录入数学公式的方法(二)

    以前推荐的方法,随着方正飞翔网站上关闭了数学公式输入法的支持也不能不用了,现在再推荐一个可以在word2010以上版中快捷输入数学公式的方法,安装AxMath,一切问题都OK!我是直接购买的正版,25 ...

  9. Python循环-break和continue

    break用于完全结束一个循环,跳出循环体,执行循环后面的语句 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" count = ...

  10. C#设计模式(9)——装饰者模式

    一.概念 装饰者模式以对客户透明的方式动态地给一个对象附加上更多的责任,装饰者模式相比生成子类可以更灵活地增加功能. 二.模型 三.代码实现 /// <summary> /// 手机抽象类 ...