-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindStringHard.cpp
151 lines (111 loc) · 3.68 KB
/
FindStringHard.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
// BEGIN CUT HERE
/*
SRM 746 Div1 Medium (500)
PROBLEM STATEMENT
An antipalindrome is a string which differs from its own reverse at all positions. For instance, "ab", "aabb", "abab", and "abbaab" are antipalindromes, whereas "aaab", "abba", "aababa", "baa", and "a" are not.
A substring is a nonempty contiguous part of a given string. For instance, "aba" is a substring of "aaba" but "aaa" isn't.
Two substrings are considered different if they are located at different positions in the string. For instance, there are two distinct substrings "aba" in the string "ababa".
You are given an int n. Find and return any string with the following properties:
Its length is at most 100.
Each character is either 'a' or 'b'.
The string has exactly n antipalindromic substrings.
DEFINITION
Class:FindStringHard
Method:withAntipalindromicSubstrings
Parameters:int
Returns:string
Method signature:string withAntipalindromicSubstrings(int n)
NOTES
-It can be shown that the answer always exists. If there are multiple solutions, you may print any of them.
-It is not necessary to minimize the length of the returned string.
CONSTRAINTS
-The integer n will be between 0 and 1000, inclusive.
EXAMPLES
0)
3
Returns: "bbaab"
The three antipalindromic substrings of "bbaab" are "bbaa", "ba", and "ab".
1)
8
Returns: "ababbaab"
The eight antipalindromic substrings of "ababbaab" are three copies of "ab", two copies of "ba", and one copy each of "abab", "bbaa", and "abbaab".
2)
139
Returns: "ababaabbaabbaaabbbaaabbbaaaabbbbaaaabbbbaaaaabbbbbaaaaabbbbbaaaaaabbbbbbaaaaaabbbbbb"
*/
// END CUT HERE
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstring>
using namespace std;
int count(const string &s) {
int cnt = 0;
int len = (int)s.length();
for (int i = 0; i < len; ++i) {
for (int j = 2; i + j <= len; j += 2) {
int ok = 1;
for (int k = 0; k < j / 2; ++k) {
if (s[i + k] == s[i + j - k - 1]) {
ok = 0;
break;
}
}
cnt += ok;
}
}
return cnt;
}
class FindStringHard {
public:
string withAntipalindromicSubstrings(int n) {
string s = "a";
while (count(s) != n) {
int X = rand() % 35;
int Y = rand() % 30;
s.resize(2 * X + Y);
for (int i = 0; i < X; ++i) { s[2 * i] = 'a'; s[2 * i + 1] = 'b'; }
for (int i = 0; i < Y; ++i) { s[2 * X + i] = "ab"[rand() % 2]; }
}
return s;
}
// BEGIN CUT HERE
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
public:
void run_test(int Case) {
int n = 0;
// test_case_0
if ((Case == -1) || (Case == n)){
int Arg0 = 3;
string Arg1 = "bbaab";
verify_case(n, Arg1, withAntipalindromicSubstrings(Arg0));
}
n++;
// test_case_1
if ((Case == -1) || (Case == n)){
int Arg0 = 8;
string Arg1 = "ababbaab";
verify_case(n, Arg1, withAntipalindromicSubstrings(Arg0));
}
n++;
// test_case_2
if ((Case == -1) || (Case == n)){
int Arg0 = 139;
string Arg1 = "ababaabbaabbaaabbbaaabbbaaaabbbbaaaabbbbaaaaabbbbbaaaaabbbbbaaaaaabbbbbbaaaaaabbbbbb";
verify_case(n, Arg1, withAntipalindromicSubstrings(Arg0));
}
n++;
}
// END CUT HERE
};
// BEGIN CUT HERE
int main() {
FindStringHard ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE