codeforces496C
Removing Columns
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.
Examples
1 10
codeforces
0
4 4
case
care
test
code
2
5 4
code
forc
esco
defo
rces
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.
sol:显然有不符合的就删掉,然后就是暴力模拟,非常蛋碎
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,ans=;
bool Can[N];
char Map[N][N];
inline bool Check()
{
int i,j;
for(j=;j<=m;j++) if(Map[][j]!='.')
{
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j])
{
// printf("%d %d %d\n",j,i,i+1);
return false;
}
}
}
return true;
}
inline void Debug()
{
puts("");
int i,j;
for(i=;i<=n;i++,puts(""))
{
for(j=;j<=m;j++) putchar(Map[i][j]);
}
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
int i,j;
R(n); R(m);
for(i=;i<=n;i++) scanf("%s",Map[i]+);
for(j=;j<=m;j++) if(Map[][j]!='.')
{
bool flg=;
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j]) {flg=; break;}
}
if(flg) break;
else for(i=;i<n;i++) if(Map[i][j]<Map[i+][j]) Can[i]=;
}
for(;;)
{
if(Check()) break;
for(j=;j<=m;j++) if(Map[][j]!='.')
{
bool flg=;
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j]) {flg=; break;}
}
if(flg)
{
for(i=;i<=n;i++) Map[i][j]='.'; break;
}
}
for(j=;j<=m;j++) if(Map[][j]!='.')
{
bool flg=;
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j]) {flg=; break;}
}
if(flg) break;
else for(i=;i<n;i++) if(Map[i][j]<Map[i+][j]) Can[i]=;
}
ans++;
}
Wl(ans);
return ;
}
/*
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 Input
10 10
ddorannorz
mdrnzqvqgo
gdtdjmlsuf
eoxbrntqdp
hribwlslgo
ewlqrontvk
nxibmnawnh
vxiwdjvdom
hyhhewmzmp
iysgvzayst
Output
1
*/
codeforces496C的更多相关文章
随机推荐
- Mysql数据库表被锁定处理
1.查进程,查找被锁表的那个进程的ID show processlist; command 为waitting的就是锁住的表,info为执行某条语句的信息,id为进程. 2.kill掉锁表的进程ID ...
- java 加密 解密 Illegal key size
java.security.InvalidKeyException: Illegal key size 今天遇到一个奇怪的问题. 自己做的加签验签功能已经没有问题了,本地测试通过,同事放到服务器上 ...
- UITableView 和 UITableViewController
UITableView:显示有多行数据的一个列. 新建一个过程:Xcode -> File -> New -> Project...,然后选择iOS -> Applicatio ...
- vue编程式导航
vue项目中使用到了组件间传值,通过路由跳转实现从产品页进入产品详情页查看功能. 使用了this.$router.push(编程式导航) product页面中:因为只需要遮住产品列表页来显示产品详情页 ...
- java 日志框架总结
在项目开发过程中,我们可以通过 debug 查找问题.而在线上环境我们查找问题只能通过打印日志的方式查找问题.因此对于一个项目而言,日志记录是一个非常重要的问题.因此,如何选择一个合适的日志记录框架也 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统-WebApi的用法与调试
1:ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-WebApi与Unity注入 使用Unity是为了使用我们后台的BLL和DAL层 2:ASP.NET MVC5+EF6+Easy ...
- Go源码编译安装
参考文档1:https://www.cnblogs.com/majianguo/p/7258975.html 参考文档2:http://www.loongson.cn/news/company/456 ...
- Python全栈开发之路 【第二篇】:Python基础之数据类型
本节内容 一.字符串 记住: 有序类型:列表,元组,字符串 ---> 都可迭代: 无序类型:字典,集合 ---> 不可迭代: 特性:不可修改 class str(object): &quo ...
- python模块详解
什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...
- 查看mysql数据库连接数、并发数相关信息
查看mysql数据库连接数.并发数相关信息. - caodongfang126的博客 - CSDN博客 https://blog.csdn.net/caodongfang126/article/det ...