#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企业微信开发_02_通讯录同步

    一.本节要点 1.获取通讯录密钥 获取方式: 登录企业微信—>管理工具—>通讯录同步助手—>开启“API接口同步”  ; 开启后,即可看到通讯录密钥,也可设置通讯录API的权限:读取 ...

  2. Bootstrap-菜单,导航,按钮

    1.下拉菜单(基本用法) 在Bootstrap框架中的下拉菜单组件是一个独立的组件,根据不同的版本,它对应的文件: ☑  LESS版本:对应的源码文件为 dropdowns.less ☑  Sass版 ...

  3. Wannafly #4 F 线路规划

    数据范围252501 劲啊 Q国的监察院是一个神秘的组织. 这个组织掌握了整个Q国的地下力量,监察着Q国的每一个人. 监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只听从其上直接司的命令 ...

  4. HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...

  5. bzoj 2084: Antisymmetry 回文自动机

    题目: Description 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作"反对称"字符串.比如00001111和010101就是反对称的 ...

  6. 【LeetCode】020. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. Android的各国语言的缩写

    语言包在android工程中的对应关系: 中文(中国):values-zh-rCN 中文(台湾):values-zh-rTW 中文(香港):values-zh-rHK 英语(美国):values-en ...

  8. windows下,CSV文件Excel打开乱码

    在windows下面,csv文件另存为UTF-8,在excel中打开中文是乱码,如果另存为utf-8 Big + BOM,Excel打开则是正常显示

  9. 洛谷P4721 【模板】分治 FFT(生成函数+多项式求逆)

    传送门 我是用多项式求逆做的因为分治FFT看不懂…… upd:分治FFT的看这里 话说这个万恶的生成函数到底是什么东西…… 我们令$F(x)=\sum_{i=0}^\infty f_ix^i,G(x) ...

  10. Azure 用户自定义路由 (User Defined Route)

    在公有云环境中,用户创建了一个Vnet,添加了若干个网段后,这几个网段是全联通的状态. 如果希望在Vnet中添加一些功能性的设备,比如防火墙.IPS.负载均衡设备等,就需要进行用户自定义路由的配置. ...