zoukankan      html  css  js  c++  java
  • codeforces496C

    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('
    ')
    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
    ",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
  • 相关阅读:
    js获取当前时间
    报错: unable to register MBean
    elasticsearch UNASSIGNED 处理
    esql
    记一次shell脚本编写及执行
    elasticsearch _script 脚本排序
    六大类常用的专业搜索工具
    动态设置所有string字段不分词
    @ResponseBody和@RequestBody使用
    Request method 'POST' not supported
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10735198.html
Copyright © 2011-2022 走看看