forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
63 lines (57 loc) · 1.43 KB
/
Copy pathA.cpp
File metadata and controls
63 lines (57 loc) · 1.43 KB
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
61
62
63
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
int run() {
int n;
cin >> n;
vector<string> mm(n);
REP(i,n) cin >> mm[i];
int res = 0;
while (true) {
int emptyCnt = 0;
REP(i,n) if (mm[i] == "") ++emptyCnt;
if (emptyCnt == n) break;
if (emptyCnt > 0) return -1;
if (mm[0] == "") return -1;
char ch = mm[0][0];
vector<int> num(n);
REP(i,n) {
while (mm[i] != "" && mm[i][0] == ch) {
++num[i];
mm[i] = mm[i].substr(1);
}
if (num[i] == 0) return -1;
}
sort(num.begin(), num.end());
int tmp = -1;
FOR(m,num.front(),num.back()) {
int s = 0;
REP(i,n) {
s += abs(num[i] - m);
}
if ((tmp == -1) || (s < tmp)) tmp = s;
}
res += tmp;
}
return res;
}
int main() {
int k;
cin >> k;
FOR(c,1,k) {
cout << "Case #" << c << ": ";
int res = run();
if (res == -1) cout << "Fegla Won" << endl;
else cout << res << endl;
}
return 0;
}