一个可能的算法来确定两个字符串是否彼此字谜?字谜、字符串、算法、两个

由网友(姐的温柔绝不喂狗)分享简介:我有这样的想法(用C语言),用于检查两个字符串是否由ASCII字母组成是彼此的字谜:I have this idea (using C language) for checking whether two strings formed from ASCII letters are anagrams of one an...

我有这样的想法(用C语言),用于检查两个字符串是否由ASCII字母组成是彼此的字谜:

I have this idea (using C language) for checking whether two strings formed from ASCII letters are anagrams of one another:

检查字符串的长度相同。

Check if the strings are the same length.

检查是否所有字符的ASCII值的总和是相同的两个字符串。

Check if the sum of the ASCII values of all chars is the same for both strings.

检查是否所有字符的ASCII值的产品是相同的两个字符串。

Check if the product of the ASCII values of all chars is the same for both strings.

我相信,如果所有这三个是正确的,那么字符串必须是彼此的字谜。但是,我不能证明这一点。有人可以帮助我证明或反驳,这将工作?

I believe that if all three are correct, then the strings must be anagrams of one another. However, I can't prove it. Can someone help me prove or disprove that this would work?

谢谢!

推荐答案

我写了一个快速程序,以强力搜索的冲突,并发现这种方法确实的不可以总是工作。琴弦ABFN和AAHM具有相同的ASCII总和和产品,但不彼此字谜。他们的ASCII码之和为279和ASCII产品是23423400。

I wrote a quick program to brute-force search for conflicts and found that this approach does not always work. The strings ABFN and AAHM have the same ASCII sum and product, but are not anagrams of one another. Their ASCII sum is 279 and ASCII product is 23,423,400.

有比这多很多的冲突。我的程序,搜索在所有长度为四根弦,发现11737冲突。

There are a lot more conflicts than this. My program, searching over all length-four strings, found 11,737 conflicts.

有关引用,这里的C ++源$ C ​​$ C:

For reference, here's the C++ source code:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {
  /* Sparse 2D table where used[sum][prod] is either nothing or is a string
   * whose characters sum to "sum" and whose product is "prod".
   */
  map<int, map<int, string> > used;

  /* List of all usable characters in the string. */
  vector<char> usable;
  for (char ch = 'A'; ch <= 'Z'; ch++) {
    usable.push_back(ch);
  }
  for (char ch = 'a'; ch <= 'z'; ch++) {
    usable.push_back(ch);
  }

  /* Brute-force search over all possible length-four strings.  To avoid
   * iterating over anagrams, the search only explores strings whose letters
   * are in increasing ASCII order.
   */
  for (int a = 0; a < usable.size(); a++) {
    for (int b = a; b < usable.size(); b++) {
      for (int c = b; c < usable.size(); c++) {
        for (int d = c; d < usable.size(); d++) {
          /* Compute the sum and product. */
          int sum  = usable[a] + usable[b] + usable[c] + usable[d];
          int prod = usable[a] * usable[b] * usable[c] * usable[d];

          /* See if we have already seen this. */
          if (used.count(sum) &&
              used[sum].count(prod)) {
            cout << "Conflict found: " << usable[a] << usable[b] << usable[c] << usable[d] << " conflicts with " << used[sum][prod] << endl;
          }

          /* Update the table. */
          used[sum][prod] = string() + usable[a] + usable[b] + usable[c] + usable[d];
        }
      }
    }
  }
}

希望这有助于!

Hope this helps!

阅读全文

相关推荐

最新文章