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
  • 相关阅读:
    转 | 禁忌搜索算法(Tabu Search)求解带时间窗的车辆路径规划问题详解(附Java代码)
    Branch and price and cut求解传统VRP问题以及VRPTW问题
    标号法(label-setting algorithm)求解带时间窗的最短路问题(ESPPRC)
    运筹学从何学起?如何快速入门精确式算法?
    转 | 模拟退火算法(SA)和迭代局部搜索(ILS)求解TSP的Java代码分享
    用Python画论文折线图、曲线图?几个代码模板轻松搞定!
    45. 截取“测试数据”后面的内容
    44. 更改oracle字符集编码american_america.zh16gbk 改为 SIMPLIFIED CHINESE_CHINA.ZHS16GBK
    18. 浏览器关闭页面时弹出“确定要离开此面吗?”
    6. concat_ws用法
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10735198.html
Copyright © 2011-2022 走看看