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的更多相关文章
随机推荐
- mongo中游标
1.手动循环访问游标 mongo中我们常用的查询方式db.collection.find()方法其实返回的就是游标,只不过我们并未给返回的游标分配变量,我们所看到的的查询数据也就是游标自动迭代得出的( ...
- Recurrent Neural Network[Quasi RNN]
0.背景 RNN模型,特别是包含着门控制的如LSTM等模型,近年来成了深度学习解决序列任务的标准结构.RNN层不但可以解决变长输入的问题,还能通过多层堆叠来增加网络的深度,提升表征能力和提升准确度.然 ...
- 拉格朗日乘子法(Lagrange Multiplier)和KKT条件
拉格朗日乘子法:对于等式约束的优化问题,求取最优值. KKT条件:对于含有不等式约束的优化问题,求取最优值. 最优化问题分类: (1)无约束优化问题: 常常使用Fermat定理,即求取的导数,然后令其 ...
- redis底层设计(一)——内部数据结构
redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- 【机器学习】SKlearn + XGBoost 预测 Titanic 乘客幸存
Titanic 数据集是从 kaggle下载的,下载地址:https://www.kaggle.com/c/titanic/data 数据一共又3个文件,分别是:train.csv,test.csv, ...
- Gradle构建工具从入门到精通(IDEA)
1.Gradle安装 官网下载压缩包,然后解压,配置本地环境变量.主要有下面两个: GRADLE_HOME 是解压后的目录, GRADLE_USER_HOME 的作用是让其他程序检测到本地.gradl ...
- NewZealand。。。
秀个存在感...
- struts2的基本使用
struts2在web中当作前端控制器,接收来自页面的请求,使用过滤器拦截模式对请求进行拦截并交给相应配置的action类处理. 所以在web中使用最重要的是struts2的核心过滤器StrutsPr ...
- C. Prefixes and Suffixes
链接 [https://codeforces.com/contest/1092/problem/C] 题意 给你某个字符串的长度n,再给你2*n-2个前缀或者后缀 让你判断那些是前缀那些是后缀 关键是 ...
- echarts各个配置项详细说明总结
https://blog.csdn.net/sinat_34492035/article/details/70258557 https://blog.csdn.net/qq_34908167/arti ...