TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
abbreviation.cpp
Go to the documentation of this file.
1
26#include <cassert>
27#include <cstdint>
28#include <iostream>
29#include <string>
30#include <vector>
35namespace dynamic_programming {
42namespace abbreviation {
59bool abbreviation_recursion(std::vector<std::vector<bool>> *memo,
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()) {
65 return true;
66 } else if (str_idx == str.size() && result_idx != result.size()) {
67 // result `t` is not converted, return false
68 return false;
69 } else if (!visited->at(str_idx).at(result_idx)) {
81 if (str[str_idx] == result[result_idx]) {
82 ans = abbreviation_recursion(memo, visited, str, result,
83 str_idx + 1, result_idx + 1);
84 } else if (str[str_idx] - 32 == result[result_idx]) {
85 ans = abbreviation_recursion(memo, visited, str, result,
86 str_idx + 1, result_idx + 1) ||
87 abbreviation_recursion(memo, visited, str, result,
88 str_idx + 1, result_idx);
89 } else {
90 // if `str[i]` is uppercase, then cannot be converted, return
91 // `false`
92 // else `str[i]` is lowercase, only option is to discard this
93 // character
94 if (str[str_idx] >= 'A' && str[str_idx] <= 'Z') {
95 ans = false;
96 } else {
97 ans = abbreviation_recursion(memo, visited, str, result,
98 str_idx + 1, result_idx);
99 }
100 }
101 }
102 (*memo)[str_idx][result_idx] = ans;
103 (*visited)[str_idx][result_idx] = true;
104 return (*memo)[str_idx][result_idx];
105}
119bool abbreviation(const std::string &str, const std::string &result) {
120 std::vector<std::vector<bool>> memo(
121 str.size() + 1, std::vector<bool>(result.size() + 1, false));
122
123 for (uint32_t i = 0; i <= str.size(); ++i) {
124 memo[i][0] = true;
125 }
126 for (uint32_t i = 1; i <= result.size(); ++i) {
127 memo[0][i] = false;
128 }
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]);
135 } else {
136 if (str[i - 1] >= 'A' && str[i - 1] <= 'Z') {
137 memo[i][j] = false;
138 } else {
139 memo[i][j] = memo[i - 1][j];
140 }
141 }
142 }
143 }
144 return memo.back().back();
145}
146} // namespace abbreviation
147} // namespace dynamic_programming
148
153static void test() {
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));
158
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));
166
167 visited = std::vector<std::vector<bool>>(
168 s.size() + 1, std::vector<bool>(t.size() + 1, false));
169
170 assert(dynamic_programming::abbreviation::abbreviation_recursion(
171 &memo, &visited, s, t) == false);
172 assert(dynamic_programming::abbreviation::abbreviation(s, t) == false);
173
174 s = "DRFNLZZVHLPZWIupjwdmqafmgkg";
175 t = "DRFNLZZVHLPZWI";
176
177 memo = std::vector<std::vector<bool>>(
178 s.size() + 1, std::vector<bool>(t.size() + 1, false));
179
180 visited = std::vector<std::vector<bool>>(
181 s.size() + 1, std::vector<bool>(t.size() + 1, false));
182
183 assert(dynamic_programming::abbreviation::abbreviation_recursion(
184 &memo, &visited, s, t) == true);
185 assert(dynamic_programming::abbreviation::abbreviation(s, t) == true);
186}
187
192int main() {
193 test(); // run self-test implementations
194 return 0;
195}
static void test()
Self test-implementations.
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.
int main()
Main function.
Functions for Abbreviation implementation.
Dynamic Programming algorithms.