Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Definitely possible, not really difficult... but this will generate lots of strings that's for sure.</p> <p>The first thing to remark is that you know how many strings it's going to generate beforehand, so it's easy to do some sanity check :)</p> <p>The second: it sounds like a recursive solution would be easy (like many traversal problems).</p> <pre><code>class CharacterMapper { public: CharacterMapper(): mGenerated(), mMapped() { for (int i = -128, max = 128; i != max; ++i) mMapped[i].push_back(i); // 'a' is mapped to 'a' by default } void addMapped(char origin, char target) { std::string&amp; m = mMapped[origin]; if (m.find(target) == std::string::npos) m.push_back(target); } // addMapped void addMapped(char origin, const std::string&amp; target) { for (size_t i = 0, max = target.size(); i != max; ++i) this-&gt;addMapped(origin, target[i]); } // addMapped void execute(const std::string&amp; original) { mGenerated.clear(); this-&gt;next(original, 0); this-&gt;sanityCheck(original); this-&gt;print(original); } private: void next(std::string original, size_t index) { if (index == original.size()) { mGenerated.push_back(original); } else { const std::string&amp; m = mMapped[original[index]]; for (size_t i = 0, max = m.size(); i != max; ++i) this-&gt;next( original.substr(0, index) + m[i] + original.substr(index+1), index+1 ); } } // next void sanityCheck(const std::string&amp; original) { size_t total = 1; for (size_t i = 0, max = original.size(); i != max; ++i) total *= mMapped[original[i]].size(); if (total != mGenerated.size()) std::cout &lt;&lt; "Failure: should have found " &lt;&lt; total &lt;&lt; " words, found " &lt;&lt; mGenerated.size() &lt;&lt; std::endl; } void print(const std::string&amp; original) const { typedef std::map&lt;char, std::string&gt;::const_iterator map_iterator; typedef std::vector&lt;std::string&gt;::const_iterator vector_iterator; std::cout &lt;&lt; "Original: " &lt;&lt; original &lt;&lt; "\n"; std::cout &lt;&lt; "Mapped: {"; for (map_iterator it = mMapped.begin(), end = mMapped.end(); it != end; ++it) if (it-&gt;second.size() &gt; 1) std::cout &lt;&lt; "'" &lt;&lt; it-&gt;first &lt;&lt; "': '" &lt;&lt; it-&gt;second.substr(1) &lt;&lt; "'"; std::cout &lt;&lt; "}\n"; std::cout &lt;&lt; "Generated:\n"; for (vector_iterator it = mGenerated.begin(), end = mGenerated.end(); it != end; ++it) std::cout &lt;&lt; " " &lt;&lt; *it &lt;&lt; "\n"; } std::vector&lt;std::string&gt; mGenerated; std::map&lt;char, std::string&gt; mMapped; }; // class CharacterMapper int main(int argc, char* argv[]) { CharacterMapper mapper; mapper.addMapped('a', "eo"); mapper.addMapped('d', "gh"); mapper.addMapped('b', "i"); mapper.execute("abba"); } </code></pre> <p>And here is the output:</p> <pre><code>Original: abba Mapped: {'a': 'eo''b': 'i''d': 'gh'} Generated: abba abbe abbo abia abie abio aiba aibe aibo aiia aiie aiio ebba ebbe ebbo ebia ebie ebio eiba eibe eibo eiia eiie eiio obba obbe obbo obia obie obio oiba oibe oibo oiia oiie oiio </code></pre> <p>Yeah, rather lengthy, but there's a lot that does not directly participate to the computation (initialization, checks, printing). The core methods is <code>next</code> which implements the recursion.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload