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的更多相关文章
随机推荐
- WPF(一)
什么是WPF WPF(Windows Presentation Foundation)是用于Windows的现代图形显示系统.与之前出现的技术相比,WPF发生了根本性变化.WPF引用了"内置 ...
- linux内存源码分析 - 内存压缩(同步关系)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 概述 最近在看内存回收,内存回收在进行同步的一些情况非常复杂,然后就想,不会内存压缩的页面迁移过程中的同步关系也 ...
- "INSTALL_FAILED_DUPLICATE_PERMISSION "错误解决
我们在进行Android组件安全测试时,如果遇到声明了权限的组件,在编写PoC时,可能会遇到如下错误提示: INSTALL_FAILED_DUPLICATE_PERMISSION perm=com.m ...
- Elasticsearch 思维导图集锦(持续更新...)
目录 引言 思维导图 全文搜索 Elastic 基础 Query DSL Multi Match Query 系列文章列表 参考 引言 本文主要是对 elasticsearch 的一些知识点使用思维导 ...
- 【开源】小程序、小游戏和Web运动引擎 to2to 发布
简单轻量跨平台的 Javascript 运动引擎 Github → https://github.com/dntzhang/cax/tree/master/packages/to Simple DEM ...
- 【记一次pull request的惨痛教训】不可见的分隔符之Zero-with-space
问题描述: 我在修改 ctf-wiki 目录后进行 mkdocs build 去生成索引目录的时候报错: 然后我尝试定位到第 2 行和第 288 行,这些行我似乎并没有修改过啊. 未果,开始去找师傅解 ...
- 剑指offer--2.替换空格
题目: 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 思路:可以使用replace或者 ...
- Python里面如何拷贝一个对象
1.赋值(=),就是创建了对象的一个新的引用,修改其中任意一个变量都会影响到另一个. In [168]: a Out[168]: [1, 2, 3] In [169]: b=a In [170]: a ...
- Tomcat集成Memcached Session Manager方案
http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.2/memcached-session-ma ...
- PHP优化与提升
一.十个不错的建议 1.使用 ip2long() 和 long2ip() 函数来把 IP 地址转化成整型存储到数据库里.这种方法把存储空间降到了接近四分之一(char(15) 的 15 个字节对整形的 ...