codeforces496C

本文解析了CodeForces-496C问题,该问题涉及从一个由小写英文字母组成的n×m矩形表格中移除列,以使剩余的行按字典序从上到下排列。通过实例说明了解决方案,并提供了详细的代码实现。

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=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=105;
int n,m,ans=0;
bool Can[N];
char Map[N][N];
inline bool Check()
{
    int i,j;
    for(j=1;j<=m;j++) if(Map[1][j]!='.')
    {
        for(i=1;i<n;i++) if(!Can[i])
        {
            if(Map[i][j]>Map[i+1][j])
            {
//                printf("%d %d %d\n",j,i,i+1);
                return false;
            }
        }
    }
    return true;
}
inline void Debug()
{
    puts("");
    int i,j;
    for(i=1;i<=n;i++,puts(""))
    {
        for(j=1;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=1;i<=n;i++) scanf("%s",Map[i]+1);
    for(j=1;j<=m;j++) if(Map[1][j]!='.')
    {
        bool flg=0;
        for(i=1;i<n;i++) if(!Can[i])
        {
            if(Map[i][j]>Map[i+1][j]) {flg=1; break;}
        }
        if(flg) break;
        else for(i=1;i<n;i++) if(Map[i][j]<Map[i+1][j]) Can[i]=1;
    }
    for(;;)
    {
        if(Check()) break;
        for(j=1;j<=m;j++) if(Map[1][j]!='.')
        {
            bool flg=0;
            for(i=1;i<n;i++) if(!Can[i])
            {
                if(Map[i][j]>Map[i+1][j]) {flg=1; break;}
            }
            if(flg)
            {
                for(i=1;i<=n;i++) Map[i][j]='.'; break;
            }
        }
        for(j=1;j<=m;j++) if(Map[1][j]!='.')
        {
            bool flg=0;
            for(i=1;i<n;i++) if(!Can[i])
            {
                if(Map[i][j]>Map[i+1][j]) {flg=1; break;}
            }
            if(flg) break;
            else for(i=1;i<n;i++) if(Map[i][j]<Map[i+1][j]) Can[i]=1;
        }
        ans++;
    }
    Wl(ans);
    return 0;
}
/*
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
*/
View Code

 

转载于:https://www.cnblogs.com/gaojunonly1/p/10735198.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值