# [Codeforces496C] Removing Columns

## 描述 Description

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.

## 输入格式 InputFormat

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.

## 输出格式 OutputFormat

Print a single number — the minimum number of columns that you need to remove in order to make the table good.

4 4
case
care
test
code

2

Codeforces 496C

## 代码 Code

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=105;
int i,j,t,n,m,l,r,k,z,y,x;
char a[maxn][maxn];
bool b[maxn];
int ans;
inline bool check(int s)
{
int i,j,t;
for (i=1;i<=n;i++) if (a[i][s]<a[i-1][s] && !b[i]) return false;
return true;
}
int main()
{
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++) scanf("%s",a[i]);
memset(b,false,sizeof(b));
for (ans=0,i=0;i<m;i++)
{
if (check(i))
{
for (j=1;j<=n;j++) if (a[j][i]>a[j-1][i]) b[j]=true;
}
else ans++;
}
printf("%d\n",ans);
return 0;
}``````