CF 496C(Removing Columns-贪心取字典序)

最小化删除列数以形成字典序递增表格
解决了一个关于优化表格列删除数量的问题,目标是通过最少的列删除操作使得表格中行的字典序形成递增顺序。通过贪心策略逐列判断并决定是否保留,最终计算所需的最小删除次数。

A. 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.

Sample test(s)
input
1 10
codeforces
output
0
input
4 4
case
care
test
code
output
2
input
5 4
code
forc
esco
defo
rces
output
4
Note

In the first sample the table is already good.

In the second sample you may remove the first and third column.

In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).

Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.


题目大意:有若干长度相等的字符串依次排列。问至少删除若干列,才能使字符串从上到下字典序单调不降。

显然,第一行如果不满足字典序一定要删。

如果满足字典序(如果前缀不同可分开考虑),那么取这一段一定不劣

(因为对于每个前缀一致的一段,

eg1:

AAABxxxx。。

AAACxxxx。。

可以看到由于前缀不同,后面x可乱取。

eg2:

AAABxxxx。。

AAABxxxx。。

前缀一样,对后面不影响还是要保证字典序。

∴贪心,从左到右每列能留就留。


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
char a[MAXN][MAXN];
int pre[MAXN];
int main()
{
//	freopen("Columns.in","r",stdin);
//	freopen(".out","w",stdout);
	int n,m;
	cin>>n>>m;
	For(i,n) scanf("%s",a[i]+1),pre[i]=1;
	int ans=0;
	For(j,m) 
	{
		bool flag=0;
		Fork(i,2,n) 
		{
			if (i==pre[i]) continue;
			if (pre[i]<i)
			{
				if (a[i][j]<a[i-1][j]) {flag=1;break;}
			}	
			
		}
		ans+=flag;
		
		if (flag==0)
		{
			Fork(i,2,n)
			{
				if (i==pre[i]) continue;
				if (pre[i]<i&&a[i][j]!=a[i-1][j]) pre[i]=i;
				else pre[i]=pre[i-1];
			}
		} 
		
	} 
	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、付费专栏及课程。

余额充值