C. Removing Columns
2016-04-21 18:16
323 查看
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
we obtain the table:
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
output
input
output
input
output
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.
解题说明:此题是一道模拟题,要求每行的字母要比下一行的字母排序靠前,问最少删除的列数是多少。可以先给设定一个初始值用来记录每列的情况,然后针对每一行判断,如果不满足条件就记录下该列位置,最后统计删除的列数。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;
int main()
{
char ar[1002][1002];
int n,m,i,j,count,ar1[1002];
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",ar[i]);
}
for(i=0;i<m;i++)
{
ar1[i]=1;
}
for(i=1;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ar1[j]==1)
{
if(ar[i][j]<ar[i-1][j])
{
ar1[j]=0;
i=0;
break;
}
if(ar[i][j]>ar[i-1][j])
{
break;
}
}
}
}
count=0;
for(i=0;i<m;i++)
{
count+=1-ar1[i];
}
printf("%d\n",count);
return 0;
}
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
解题说明:此题是一道模拟题,要求每行的字母要比下一行的字母排序靠前,问最少删除的列数是多少。可以先给设定一个初始值用来记录每列的情况,然后针对每一行判断,如果不满足条件就记录下该列位置,最后统计删除的列数。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;
int main()
{
char ar[1002][1002];
int n,m,i,j,count,ar1[1002];
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",ar[i]);
}
for(i=0;i<m;i++)
{
ar1[i]=1;
}
for(i=1;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ar1[j]==1)
{
if(ar[i][j]<ar[i-1][j])
{
ar1[j]=0;
i=0;
break;
}
if(ar[i][j]>ar[i-1][j])
{
break;
}
}
}
}
count=0;
for(i=0;i<m;i++)
{
count+=1-ar1[i];
}
printf("%d\n",count);
return 0;
}
相关文章推荐
- 【编译原理】求First集和Follow集
- Http 请求原理简介
- linux异步IO编程实例分析
- android学习笔记——Socket简单介绍
- android学习笔记——蓝牙介绍
- CSS中font-style的属性有Italic oblique,它们俩的区别是什么呢?
- ZOJ 3609 Modular Inverse【同余问题】
- 程序媛的好习惯
- 中间表--简例说明
- iOS环信3.0集成 (二)UI文件集成
- C和指针第四章编程练习
- android学习笔记——MVP模式介绍
- Android adb logcat使用技巧
- 数字图像处理,相位相关图像配准算法的C++实现
- CardView,android代码设置颜色没效果,解决
- SharePoint 2013 创建我的站点、onedrive
- Android 6.0 动态权限申请注意事项
- iOS开发 贝塞尔曲线UIBezierPath
- 使用kickstart脚本安装CentOS7
- innodb的锁到底占用多少内存