CodeForces - 1072D (2020.5.21训练G题)

这篇博客介绍了一个关于矩阵的算法问题,矩阵大小为n×n,允许改变不超过k个字母。任务是找到从左上角到右下角路径中字典序最小的字符串。解决方案是通过贪心策略,首先找出路径上的所有可能的a,然后在允许的更改次数内将其替换为a,从而确保字典序最小。
摘要由CSDN通过智能技术生成

Problem
You are given a matrix of size n×n filled with lowercase English letters. You can change no more than kletters in this matrix.

Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is 2n−1

Find the lexicographically smallest string that can be associated with a path after changing letters in at most k cells of the matrix.A string ais lexicographically smaller than a string b, if the first different letter in a and b is smaller in a
Input
The first line contains two integers nand k (1≤n≤2000, 0≤k≤n2) — the size of the matrix and the number of letters you can change.Each of the next n lines contains a string of n lowercase English letters denoting one row of the matrix.
Output
Output the lexicographically smallest string that can be associated with some valid path after changing no more than k letters in the matrix.

题意:有一个n*n的矩阵,有k次将字母变a的机会,从左上角到右下角,每次只能右移或下移,求一个字典序最小的轨迹

字典序最小,贪心的想那一定是尽可能将多的a安排在轨迹靠前的位置,那我们就预处理出靠前位置的所有a,直接将该位置换成a即可,之后就走一步贪一步就好

AC代码

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 2005;
char s[maxn][maxn];
int vis[maxn][maxn];
int f[maxn][maxn];
int n, k;
int main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值