您的位置:首页 > 其它

C. Removing Columns

2016-04-21 18:16 323 查看
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.

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.

解题说明:此题是一道模拟题,要求每行的字母要比下一行的字母排序靠前,问最少删除的列数是多少。可以先给设定一个初始值用来记录每列的情况,然后针对每一行判断,如果不满足条件就记录下该列位置,最后统计删除的列数。

#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;

int main()
{
char ar[1002][1002];
int n,m,i,j,count,ar1[1002];
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",ar[i]);
}
for(i=0;i<m;i++)
{
ar1[i]=1;
}
for(i=1;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ar1[j]==1)
{
if(ar[i][j]<ar[i-1][j])
{
ar1[j]=0;
i=0;
break;
}
if(ar[i][j]>ar[i-1][j])
{
break;
}
}
}
}
count=0;
for(i=0;i<m;i++)
{
count+=1-ar1[i];
}
printf("%d\n",count);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: