-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimilarNames2.cpp
172 lines (166 loc) · 4.7 KB
/
SimilarNames2.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#line 3 "SimilarNames2.cpp"
#include <string>
#include <vector>
using namespace std;
class SimilarNames2 {
public:
vector<int> graph[51];
int l;
const long long MOD = 1000000007;
long long dp[51][51];
long long dfs(int v, int c) {
if (dp[v][c] != -1)
return dp[v][c];
if (c == l)
return 1;
dp[v][c] = 0;
for (int i = 0; i < graph[v].size(); i++)
dp[v][c] += dfs(graph[v][i], c+1);
return dp[v][c];
}
int count(vector <string> names, int L) {
l = L;
for (int i = 0; i < 51; i++) {
graph[i].clear();
for (int j = 0; j < 51; j++)
dp[i][j] = -1;
}
for (int i = 0; i < names.size(); i++) {
for (int j = 0; j < names.size(); j++) {
if (i == j || names[i].size() > names[j].size())
continue;
bool ok = true;
for (int k = 0; k < names[i].size(); k++)
if (names[i][k] != names[j][k])
ok = false;
if (ok)
graph[i].push_back(j);
}
}
long long res = 0;
for (int i = 0; i < names.size(); i++)
res += dfs(i, 1);
for (int i = 2; i <= names.size()-L; i++) {
res *= i;
res %= MOD;
}
return res;
}
};
// BEGIN CUT HERE
#include <ctime>
#include <cmath>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
if (argc == 1)
{
cout << "Testing SimilarNames2 (950.0 points)" << endl << endl;
for (int i = 0; i < 20; i++)
{
ostringstream s; s << argv[0] << " " << i;
int exitCode = system(s.str().c_str());
if (exitCode)
cout << "#" << i << ": Runtime Error" << endl;
}
int T = time(NULL)-1396788645;
double PT = T/60.0, TT = 75.0;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
cout << endl;
cout << "Time : " << T/60 << " minutes " << T%60 << " secs" << endl;
cout << "Score : " << 950.0*(.3+(.7*TT*TT)/(10.0*PT*PT+TT*TT)) << " points" << endl;
}
else
{
int _tc; istringstream(argv[1]) >> _tc;
SimilarNames2 _obj;
int _expected, _received;
time_t _start = clock();
switch (_tc)
{
case 0:
{
string names[] = {"kenta", "kentaro", "ken"};
int L = 2;
_expected = 3;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}
case 1:
{
string names[] = {"hideo", "hideto", "hideki", "hide"};
int L = 2;
_expected = 6;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}
case 2:
{
string names[] = {"aya", "saku", "emi", "ayane", "sakura", "emika", "sakurako"};
int L = 3;
_expected = 24;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}
case 3:
{
string names[] = {"taro", "jiro", "hanako"};
int L = 2;
_expected = 0;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}
case 4:
{
string names[] = {"alice", "bob", "charlie"};
int L = 1;
_expected = 6;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}
case 5:
{
string names[] = {"ryota", "ryohei", "ryotaro", "ryo", "ryoga", "ryoma", "ryoko", "ryosuke", "ciel", "lun",
"ryuta", "ryuji", "ryuma", "ryujiro", "ryusuke", "ryutaro", "ryu", "ryuhei", "ryuichi", "evima"};
int L = 3;
_expected = 276818566;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}
/*case 6:
{
string names[] = ;
int L = ;
_expected = ;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}*/
/*case 7:
{
string names[] = ;
int L = ;
_expected = ;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}*/
/*case 8:
{
string names[] = ;
int L = ;
_expected = ;
_received = _obj.count(vector <string>(names, names+sizeof(names)/sizeof(string)), L); break;
}*/
default: return 0;
}
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
double _elapsed = (double)(clock()-_start)/CLOCKS_PER_SEC;
if (_received == _expected)
cout << "#" << _tc << ": Passed (" << _elapsed << " secs)" << endl;
else
{
cout << "#" << _tc << ": Failed (" << _elapsed << " secs)" << endl;
cout << " Expected: " << _expected << endl;
cout << " Received: " << _received << endl;
}
}
}
// END CUT HERE