-
Notifications
You must be signed in to change notification settings - Fork 0
/
ikeyboard.cpp
94 lines (86 loc) · 1.8 KB
/
ikeyboard.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
#define pr pair<char,char>
using namespace std;
pr result[100];
int c=10000;
void print(pr list[100], int l){
//cout<<"hi";
for(int i=0; i<l; i++){
cout<<i+1<<": ";
if(list[i].first == list[i].second)
cout<<list[i].first<<endl;
else
cout<<list[i].first<<"-"<<list[i].second<<endl;
}
cout<<endl;
}
int minstrokes(int arr[100], pr list[100], int l, int level, char ub, int cost){
if(l == level && list[l-1].second != ub){
//cout<<cost<<" ";
//print(list,l);
return 10000;
}
else if(l>0 && list[l-1].second == ub){
//cout<<"cost = "<<cost<<endl;
//print(list,l);
if(c>cost){
c = cost;
for(int i=0; i<l; i++)
result[i] = list[i];
}
return 0;
}
else{
int sum = 10000;
char k;
if(l==0)
k = 'a';
else
k = (char)(list[l-1].second + 1);
char ch=k;
for(ch = k; ch <= ub; ch++){
int s = 0;
pr p;
p.first = k;
p.second = ch;
list[l] = p;
//print(list, l);
//cout<<list[l].first<<" "<<list[l].second<<" "<<ub<<endl;
for(char charc = k; charc <= ch; charc++){
int j;
for(j=0; !(charc >= list[j].first && charc <= list[j].second); j++);
s += (charc - list[j].first + 1) * arr[charc - 'a'];
//cout<<s<<" "<<charc-list[j].first+1<<" "<<list[j].first<<" "<<list[j].second<<" ";
}
//cout<<" "<<k<<endl;
s += minstrokes(arr, list, l+1, level, ub, cost + s);
if(sum > s){
sum = s;
}
list[l].first = '?';
list[l].second = '?';
}
return sum;
}
}
int main()
{
pr list[100];
int arr[100]; /*= {20, 5, 7, 18, 20,
4, 2, 4, 4, 99,
4, 4, 2, 2, 23,
45, 4, 4, 9, 4,
5, 4, 44, 4, 4, 7};*/
char ch;
cin >> ch;
for(int i=0; i<26; i++)
arr[i] = 5;
cout<<endl;
int k;
cin>>k;
cout << endl << minstrokes(arr, list, 0, k, ch, 0) << endl;
print(result, k);
cout<<"cost is "<<c<<endl;
return 0;
}