「CF497A」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
1 2 3 |
abcd edfg hijk |
we obtain the table:
1 2 3 |
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.
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.
Print a single number — the minimum number of columns that you need to remove in order to make the table good.
1 2 |
1 10 codeforces |
1 |
1 2 3 4 5 |
4 4 case care test code |
1 |
2 |
1 2 3 4 5 6 |
5 4 code forc esco defo rces |
1 |
4 |
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.
题解
从左到右考虑每一列
如果剩下的元素非递增的话删这列
否则删除所有严格大于前一个的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> #include<ctime> #include<vector> #include<queue> #include<algorithm> #include<map> #define inf 1000000000 #define pa pair<int,int> #define ll long long using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,ans; char a[1005][1005],x[1005]; bool del[1005]; int main() { n=read();m=read(); for(int i=1;i<=n;i++) { scanf("%s",x); for(int j=0;j<m;j++) a[j][i-1]=x[j]; } for(int i=0;i<m;i++) { int flag=0; for(int j=1;j<n;j++) if(!del[j]&&a[i][j]<a[i][j-1]){flag=1;break;} if(flag)ans++; else { for(int j=0;j<n;j++) if(a[i][j]!=a[i][j-1]) del[j]=1; } } printf("%d\n",ans); return 0; } |