codeforces496C
Removing Columns
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
1 10
codeforces
0
4 4
case
care
test
code
2
5 4
code
forc
esco
defo
rces
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=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,ans=;
bool Can[N];
char Map[N][N];
inline bool Check()
{
int i,j;
for(j=;j<=m;j++) if(Map[][j]!='.')
{
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j])
{
// printf("%d %d %d\n",j,i,i+1);
return false;
}
}
}
return true;
}
inline void Debug()
{
puts("");
int i,j;
for(i=;i<=n;i++,puts(""))
{
for(j=;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=;i<=n;i++) scanf("%s",Map[i]+);
for(j=;j<=m;j++) if(Map[][j]!='.')
{
bool flg=;
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j]) {flg=; break;}
}
if(flg) break;
else for(i=;i<n;i++) if(Map[i][j]<Map[i+][j]) Can[i]=;
}
for(;;)
{
if(Check()) break;
for(j=;j<=m;j++) if(Map[][j]!='.')
{
bool flg=;
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j]) {flg=; break;}
}
if(flg)
{
for(i=;i<=n;i++) Map[i][j]='.'; break;
}
}
for(j=;j<=m;j++) if(Map[][j]!='.')
{
bool flg=;
for(i=;i<n;i++) if(!Can[i])
{
if(Map[i][j]>Map[i+][j]) {flg=; break;}
}
if(flg) break;
else for(i=;i<n;i++) if(Map[i][j]<Map[i+][j]) Can[i]=;
}
ans++;
}
Wl(ans);
return ;
}
/*
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
*/
codeforces496C的更多相关文章
随机推荐
- C语言中数组变量和指针变量
指针变量为什么需要类型? 数组变量和指针变量在使用sizeof时不同,sizeof(数组变量)是数组长度,sizeof(指针变量)是存储int的字节长度4或者8(64bit). 数组变量在参数传递中, ...
- 简单的词法设计——DFA模拟程序
实验一.简单的词法设计--DFA模拟程序 一.实验目的 通过实验教学,加深学生对所学的关于编译的理论知识的理解,增强学生对所学知识的综合应用能力,并通过实践达到对所学的知识进行验证.通过对 DFA 模 ...
- 《React Native 精解与实战》书籍连载「React 与 React Native 简介」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- Xamarin Android ListView 控件使用
在项目中通常用到了ListView控件,示例如下: create the listitem class ,eg; public class ColorItem { public string Colo ...
- vue中使用sass
1.npm安装 npm install sass-loader --save-dev npm install node-sass --save-dev //--save写入到package.json里 ...
- Python-递归初识-50
#递归函数 # 了解什么是递归 : 在函数中调用自身函数 # 最大递归深度默认是997/998 —— 是python从内存角度出发做得限制 # 能看懂递归 # 能知道递归的应用场景 # 初识递归 —— ...
- javascript与php与python的函数写法区别与联系
1.javascript函数写法种类: (一).第一种 function test(param){ return 111; } (二).第二种 var test = function(param){ ...
- sqlserver2008 传入的表格格式数据流(tds)协议流不正确。
起因是在sql 2008 里使用 sql prompt 报了一个内部连接致命错误,原本应该想到是数据库连接问题的,奇怪的是能连接上数据库也能查询表仅仅是用不了工具没有智能提示. 几经查询无果,度娘上之 ...
- openstack-KVM安装与使用
一.KVM安装 1.安装条件 VT-x BIOS Intel9R) Virtualization Tech [Enabled] cat /proc/cpuinfo | grep -e vmx -e n ...
- Python_面向对象_单例模式
class A(object): pass a1 = A() a2 = A() print(a1 == a2)print(id(a1))print(id(a2)) 结果: False 23257231 ...