Removing Columns - CodeForces 496 C

本文介绍了一个算法问题的解决方案,该问题要求通过删除矩阵中的某些列,使得剩余部分按照字典序从上至下排序。文章详细解释了实现思路及步骤,并提供了完整的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Removing Columns
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table

abcd
edfg
hijk

 

we obtain the table:

acd
efg
hjk

 

A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.

Input

The first line contains two integers  — n and m (1 ≤ n, m ≤ 100).

Next n lines contain m small English letters each — the characters of the table.

Output

Print a single number — the minimum number of columns that you need to remove in order to make the table good.

题意:

题意的话比较简单,就是有n*m的字符串矩阵,可以删除任意列的字符,要求删除完后整个矩阵从上到下的每一行要按字典序排列;

思路:

按照字符串矩阵建立一个dp矩阵,可以从第二行开始,每一行都和上一行进行比较,如果不符合字典序的话就做下标记。然后从第二行开始遍历,如果遇到这一行的字符小于上一行的就break;如果遇到这一行的字符和上一行的字符相同的就继续循环,如果遇到有标记的就在这一列下标记,说明这一列已经被删除了,下一次遇到有标记的就跳过。


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N = 2e2+9;
char vec[MAX_N][MAX_N];
int res[MAX_N][MAX_N];
int out[MAX_N];
int main()
{
    int N,M,T;
    while(cin>>N>>M)
    {
        memset(res,0,sizeof(res));
        memset(out,0,sizeof(out));
        for(int i=0; i<N; i++)
        {
            scanf("%s",&vec[i]);
        }
        if(N==1)
        {
            cout<<"0"<<endl;
            continue;
        }
        for(int i=1; i<N; i++)
        {
            for(int j=0; j<M; j++)
            {
                if(vec[i][j] < vec[i-1][j])
                {
                    res[i][j] = 1;
                    //cout<<i<<"........"<<j<<endl;
                }
            }
        }
        while(1)
        {
            bool update = true;
            for(int i=1; i<N; i++)
            {
                for(int j=0; j<M; j++)
                {
                    if(out[j] == 1) continue;
                    if(res[i][j])
                    {
                        update = false;
                        out[j] = 1;
                        //cout<<j<<"!!!!!"<<endl;
                    }
                    if(!res[i][j] && vec[i][j] != vec[i-1][j]) break;
                }
            }
            if(update) break;
        }
        int ans = 0;
        for(int i=0; i<M; i++)
        {
            ans += out[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


root@sw:~/Serve/vscode# chmod 777 uninstall.sh root@sw:~/Serve/vscode# ./uninstall.sh Reading package lists... Done Building dependency tree... Done Reading state information... Done No apt package "code", but there is a snap with that name. Try "snap install code" E: Unable to locate package code Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages will be REMOVED: libauthen-sasl-perl* libclone-perl* libdata-dump-perl* libencode-locale-perl* libfile-basedir-perl* libfile-desktopentry-perl* libfile-listing-perl* libfile-mimeinfo-perl* libfont-afm-perl* libhtml-form-perl* libhtml-format-perl* libhtml-parser-perl* libhtml-tagset-perl* libhtml-tree-perl* libhttp-cookies-perl* libhttp-daemon-perl* libhttp-date-perl* libhttp-message-perl* libhttp-negotiate-perl* libio-html-perl* libio-socket-ssl-perl* libio-stringy-perl* libipc-system-simple-perl* liblwp-mediatypes-perl* liblwp-protocol-https-perl* libmailtools-perl* libnet-dbus-perl* libnet-http-perl* libnet-smtp-ssl-perl* libnet-ssleay-perl* libproxychains3* libtie-ixhash-perl* libtimedate-perl* libtry-tiny-perl* liburi-perl* libwww-perl* libwww-robotrules-perl* libx11-protocol-perl* libxml-parser-perl* libxml-twig-perl* libxml-xpathengine-perl* mesa-utils-bin* perl-openssl-defaults* xdg-utils* 0 upgraded, 0 newly installed, 44 to remove and 26 not upgraded. After this operation, 8,426 kB disk space will be freed. (Reading database ... 110336 files and directories currently installed.) Removing libauthen-sasl-perl (2.1600-1.1) ... Removing libclone-perl (0.45-1build3) ... Removing libdata-dump-perl (1.25-1) ... Removing libnet-dbus-perl (1.2.0-1build3) ... Removing libxml-twig-perl (1:3.52-1) ... Removing libxml-parser-perl:amd64 (2.46-3build1) ... Removing libhttp-daemon-perl (6.13-1ubuntu0.1) ... Removing libfile-mimeinfo-perl (0.31-1) ... Removing libfile-desktopentry-perl (0.22-2) ... Removing libfile-basedir-perl (0.09-1) ... Removing libhtml-format-perl (2.12-1.1) ... Removing libfont-afm-perl (1.20-3) ... Removing libhtml-form-perl (6.07-1) ... Removing libmailtools-perl (2.21-1) ... Removing libnet-smtp-ssl-perl (1.04-1) ... Removing libio-stringy-perl (2.111-3) ... Removing libipc-system-simple-perl (1.30-1) ... Removing libproxychains3:amd64 (3.1-9) ... Removing libtie-ixhash-perl (1.23-2.1) ... Removing libx11-protocol-perl (0.56-7.1) ... Removing libxml-xpathengine-perl (0.14-1) ... Removing mesa-utils-bin:amd64 (8.4.0-1ubuntu1) ... Removing xdg-utils (1.1.3-4.1ubuntu3~22.04.1) ... Removing liblwp-protocol-https-perl (6.10-1) ... Removing libwww-perl (6.61-1) ... Removing libfile-listing-perl (6.14-1) ... Removing libhtml-tree-perl (5.07-2) ... Removing libhtml-parser-perl:amd64 (3.76-1build2) ... Removing libhtml-tagset-perl (3.20-4) ... Removing libhttp-cookies-perl (6.10-1) ... Removing libhttp-negotiate-perl (6.01-1) ... Removing libio-socket-ssl-perl (2.074-2) ... Removing libnet-http-perl (6.22-1) ... Removing libnet-ssleay-perl:amd64 (1.92-1build2) ... Removing libtry-tiny-perl (0.31-1) ... Removing libwww-robotrules-perl (6.02-1) ... Removing perl-openssl-defaults:amd64 (5build2) ... Removing libhttp-message-perl (6.36-1) ... Removing libencode-locale-perl (1.05-1.1) ... Removing libhttp-date-perl (6.05-1) ... Removing libio-html-perl (1.004-2) ... Removing liblwp-mediatypes-perl (6.04-1) ... Removing libtimedate-perl (2.3300-2) ... Removing liburi-perl (5.10-1) ... Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.9) ... Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu jammy InRelease Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-updates InRelease [128 kB] Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-backports InRelease Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-security InRelease [129 kB] Ign:5 https://download.docker.com/linux/ubuntu jammy InRelease Hit:5 https://download.docker.com/linux/ubuntu jammy InRelease Hit:6 https://packages.emqx.com/emqx/emqx/ubuntu jammy InRelease Fetched 257 kB in 3s (94.1 kB/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done 26 packages can be upgraded. Run 'apt list --upgradable' to see them. Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)). 验证结果: [成功] VSCode二进制文件已移除 [警告] 存在残留进程 root@sw:~/Serve/vscode# 怎么解决?
最新发布
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值