-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cc
60 lines (56 loc) · 1.74 KB
/
Solution.cc
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
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
#define MAX 101
int main(int argc, char *argv[]) {
int n, m;
char mat[MAX][MAX] = {0};
vector<vector<int> > groups;
groups.push_back(vector<int>());
scanf("%d %d", &n, &m);
for (int i=0; i < n; ++i) {
scanf("%s", mat[i]);
groups[0].push_back(i);
}
int result = 0;
for (int col=0; col < m; ++col) {
bool is_sorted = true;
vector<vector<int> > new_groups;
for (int i=0; i < groups.size(); ++i) {
for (int j=1; j < groups[i].size(); ++j) {
if (mat[groups[i][j]][col] < mat[groups[i][j-1]][col]) {
is_sorted = false;
break;
}
}
if (!is_sorted) {
break;
} else {
vector<int> new_group;
for (int j=1; j < groups[i].size(); ++j) {
if (mat[groups[i][j]][col] == mat[groups[i][j-1]][col]) {
new_group.push_back(groups[i][j-1]);
} else {
if (new_group.size() > 0) {
new_group.push_back(groups[i][j-1]);
new_groups.push_back(new_group);
new_group.clear();
}
}
}
if (new_group.size() > 0) {
new_group.push_back(groups[i][groups[i].size()-1]);
new_groups.push_back(new_group);
}
}
}
if (!is_sorted) {
++result;
} else {
groups = new_groups;
}
}
printf("%d\n", result);
return 0;
}