Removing Columns

CodeForces - 496C

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

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.

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的更多相关文章

随机推荐

  1. 【mongoDB查询进阶】聚合管道(二) -- 阶段操作符

    https://segmentfault.com/a/1190000010826809 什么是管道操作符(Aggregation Pipeline Operators) mongoDB有4类操作符用于 ...

  2. [转]关于oracle sql语句查询时表名和字段名要加双引号的问题

    oracle初学者一般会遇到这个问题.   用navicat可视化创建了表,可是就是不能查到!   后来发现②语句可以查询到 ①select * from user; 但是,我们如果给user加上双引 ...

  3. JSOUP如何优秀的下载JPEG等二进制图像

    引言 JSOUP默认是不支持解析JPEG等二进制图像的,解决方法也很简单,只需要加上Jsoup.ignoreContentType(true)这一行代码就可以.关于这一点的原因,来看看官方API说明. ...

  4. Python 学习 第十三篇:数据的读写-文件、DataFrame、json

    Python的文件是一个重要的对象,使用open()函数来打开文件,创建文件对象,进行文件的读写操作.当数据用于交换信息时,通常需要把数据保存为有格式的文本数据,可以保存为有特定的行分隔符和列分隔符的 ...

  5. navicat 和 pymysql

    ---------------------------------------------------相信时间的力量,单每月经过努力的时间,一切的安排都是懊脑的安排. # # ------------ ...

  6. 爬虫(二)之scrapy框架

    01-scrapy介绍 02-项目的目录结构: scrapy.cfg 项目的主配置信息.(真正爬虫相关的配置信息在settings.py 文件中) items.py 设置数据存储模板,用于结构化数据, ...

  7. H5 后代选择器

    12-后代选择器 我是段落 我是段落 我是段落 我是段落 我是段落 我是段落 <!DOCTYPE html> <html lang="en"> <he ...

  8. POJ - 2528 区间离散化,线段树区间修改,区间询问

    这个题非常有意思的地方是,我们发现区间[1,4]和[5,8]是紧挨着的,因为这个的数代表的是一段区间,原本我们对于普通的离散, a[1]=1,a[2]=5,a[3]=6,a[4]=8;数组下标就是重新 ...

  9. Streaming Principal Component Analysis in Noisy Settings

    论文背景: 面对来袭的数据,连续样本不一定是不相关的,甚至不是同分布的. 当前,大部分在线PCA都只关注准确性,而忽视时效性! 噪声?数据缺失,观测有偏,重大异常? 论文内容: Section 2 O ...

  10. Python里面如何拷贝一个对象

    1.赋值(=),就是创建了对象的一个新的引用,修改其中任意一个变量都会影响到另一个. In [168]: a Out[168]: [1, 2, 3] In [169]: b=a In [170]: a ...