Codeforces Round #283 (Div. 1)解题报告A.B.C.

71 阅读2分钟

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 Input

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

\

\

删除若干列之后,使行为案字典序排列。

可能有疑问的是第二组数据,删除第一列没问题,第三列也没问题,主要是第四列为什么不删。

是因为它已经被标记了,所谓的字典序不要忽略了,如果前面的以一个字母小,无论后边如何不必过问了。

\

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define MAX 16000
#define inf 0x3f3f3f3f
using namespace std;
bool vis[110][110];
char Map[110][110];
int n,m;

bool fi(int i)
{
    int j;
    for(j=0;j<n-1;j++)
    {
        if(!vis[j][j+1])
        {
            if(Map[j][i]>Map[j+1][i])//如果没有被标记,并且前行大于后行必然要删除此列。
            return true;
        }
    }
    for(j=0;j<n-1;j++)
    {
        if(!vis[j][j+1])
        {
            if(Map[j][i]<Map[j+1][i])//如果前边的字母大小明确,则符合,不删除(若相等仍然返回比较下一列)
            vis[j][j+1]=true;
        }
    }
    return false;
}

int main()
{
    int i,j,k;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0; i<n; i++)
        {
            scanf("%s",Map[i]);
        }
        memset(vis,false,sizeof(false));
        int ans=0;
        for(i=0; i<m; i++)
        {
            if(fi(i) )
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


\