60 std::vector<std::vector<bool>> *visited,
61 const std::string &str,
const std::string &result,
62 uint32_t str_idx = 0, uint32_t result_idx = 0) {
63 bool ans = memo->at(str_idx).at(result_idx);
64 if (str_idx == str.size() && result_idx == result.size()) {
66 }
else if (str_idx == str.size() && result_idx != result.size()) {
69 }
else if (!visited->at(str_idx).at(result_idx)) {
81 if (str[str_idx] == result[result_idx]) {
83 str_idx + 1, result_idx + 1);
84 }
else if (str[str_idx] - 32 == result[result_idx]) {
86 str_idx + 1, result_idx + 1) ||
88 str_idx + 1, result_idx);
94 if (str[str_idx] >=
'A' && str[str_idx] <=
'Z') {
98 str_idx + 1, result_idx);
102 (*memo)[str_idx][result_idx] = ans;
103 (*visited)[str_idx][result_idx] =
true;
104 return (*memo)[str_idx][result_idx];
120 std::vector<std::vector<bool>> memo(
121 str.size() + 1, std::vector<bool>(result.size() + 1,
false));
123 for (uint32_t i = 0; i <= str.size(); ++i) {
126 for (uint32_t i = 1; i <= result.size(); ++i) {
129 for (uint32_t i = 1; i <= str.size(); ++i) {
130 for (uint32_t j = 1; j <= result.size(); ++j) {
131 if (str[i - 1] == result[j - 1]) {
132 memo[i][j] = memo[i - 1][j - 1];
133 }
else if (str[i - 1] - 32 == result[j - 1]) {
134 memo[i][j] = (memo[i - 1][j - 1] || memo[i - 1][j]);
136 if (str[i - 1] >=
'A' && str[i - 1] <=
'Z') {
139 memo[i][j] = memo[i - 1][j];
144 return memo.back().back();
154 std::string s =
"daBcd", t =
"ABC";
155 std::vector<std::vector<bool>> memo(s.size() + 1,
156 std::vector<bool>(t.size() + 1,
false)),
157 visited(s.size() + 1, std::vector<bool>(t.size() + 1,
false));
159 assert(dynamic_programming::abbreviation::abbreviation_recursion(
160 &memo, &visited, s, t) ==
true);
161 assert(dynamic_programming::abbreviation::abbreviation(s, t) ==
true);
162 s =
"XXVVnDEFYgYeMXzWINQYHAQKKOZEYgSRCzLZAmUYGUGILjMDET";
163 t =
"XXVVDEFYYMXWINQYHAQKKOZEYSRCLZAUYGUGILMDETQVWU";
164 memo = std::vector<std::vector<bool>>(
165 s.size() + 1, std::vector<bool>(t.size() + 1,
false));
167 visited = std::vector<std::vector<bool>>(
168 s.size() + 1, std::vector<bool>(t.size() + 1,
false));
170 assert(dynamic_programming::abbreviation::abbreviation_recursion(
171 &memo, &visited, s, t) ==
false);
172 assert(dynamic_programming::abbreviation::abbreviation(s, t) ==
false);
174 s =
"DRFNLZZVHLPZWIupjwdmqafmgkg";
175 t =
"DRFNLZZVHLPZWI";
177 memo = std::vector<std::vector<bool>>(
178 s.size() + 1, std::vector<bool>(t.size() + 1,
false));
180 visited = std::vector<std::vector<bool>>(
181 s.size() + 1, std::vector<bool>(t.size() + 1,
false));
183 assert(dynamic_programming::abbreviation::abbreviation_recursion(
184 &memo, &visited, s, t) ==
true);
185 assert(dynamic_programming::abbreviation::abbreviation(s, t) ==
true);
bool abbreviation_recursion(std::vector< std::vector< bool > > *memo, std::vector< std::vector< bool > > *visited, const std::string &str, const std::string &result, uint32_t str_idx=0, uint32_t result_idx=0)
Recursive Dynamic Programming function.