Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 确定两个字符串是否为彼此置换的程序的时间复杂度_C++_Algorithm_Time Complexity_Hashtable_Permutation - Fatal编程技术网

C++ 确定两个字符串是否为彼此置换的程序的时间复杂度

C++ 确定两个字符串是否为彼此置换的程序的时间复杂度,c++,algorithm,time-complexity,hashtable,permutation,C++,Algorithm,Time Complexity,Hashtable,Permutation,我编写了一个程序来确定两个字符串是否是彼此的排列。我正试图使用哈希表来实现这一点。这是我的密码: bool permutation(string word1, string word2) { unordered_map<char, int> myMap1; unordered_map<char, int> myMap2; int count1 = 0; int count2 = 0; if (word1.length() ==

我编写了一个程序来确定两个字符串是否是彼此的排列。我正试图使用哈希表来实现这一点。这是我的密码:

bool permutation(string word1, string word2) {

    unordered_map<char, int> myMap1;
    unordered_map<char, int> myMap2;
    int count1 = 0;
    int count2 = 0;

    if (word1.length() == word2.length()) {
        for (int i = 0; i < word1.length(); i++) {
            count1++;
            count2++;
            for (int j = 0; j < word1.length(); j++) {
                if (word1[i] == word1[j] && myMap1.find(word1[i]) == myMap1.end()) {
                    count1++;
                }
                if (word2[i] == word2[j] && myMap2.find(word1[i]) == myMap2.end()) {
                    count2++;
                }
            }
            myMap1.insert({word1[i], count1});
            myMap2.insert({word2[i], count2});
        }
    }
    else {
        return false;
    }
    return (myMap1.size() == myMap2.size());
}

int main() {

    string word1;
    string word2;
    getline(cin, word1);
    getline(cin, word2);

    bool result = permutation(word1, word2);

    return 0;
}
bool置换(字符串word1、字符串word2){
无序地图myMap1;
无序地图myMap2;
int count1=0;
int count2=0;
if(word1.length()==word2.length()){
for(int i=0;i
我相信上面代码的时间复杂度是O(n^2)。我想不出一个不使用嵌套循环的算法。有没有一种使用哈希表更快的方法

是的

#include <climits>
#include <iostream>
#include <unordered_map>

namespace {

bool permutation(const std::string& word1, const std::string& word2) {
  std::unordered_map<char, std::size_t> freqdiff;
  // alternatively, std::size_t freqdiff[UCHAR_MAX + 1] = {};
  for (char c : word1) {
    // alternatively, freqdiff[(unsigned char)c]++;
    freqdiff[c]++;
  }
  for (char c : word2) {
    // alternatively, freqdiff[(unsigned char)c]--;
    freqdiff[c]--;
  }
  for (auto i : freqdiff) {
    // alternatively, i != 0
    if (i.second != 0) {
      return false;
    }
  }
  return true;
}

bool permutation_with_array(const std::string& word1,
                            const std::string& word2) {
  std::size_t freqdiff[UCHAR_MAX + 1] = {};
  for (char c : word1) {
    freqdiff[static_cast<unsigned char>(c)]++;
  }
  for (char c : word2) {
    freqdiff[static_cast<unsigned char>(c)]--;
  }
  for (std::size_t i : freqdiff) {
    if (i != 0) {
      return false;
    }
  }
  return true;
}
}

int main() {
  std::string word1;
  std::string word2;
  std::getline(std::cin, word1);
  std::getline(std::cin, word2);
  std::cout << permutation(word1, word2) << '\n';
  std::cout << permutation_with_array(word1, word2) << '\n';
}
#包括
#包括
#包括
名称空间{
布尔置换(常量std::string和word1,常量std::string和word2){
std::无序映射频率差;
//或者,std::size_t freqdiff[UCHAR_MAX+1]={};
for(字符c:word1){
//或者,freqdiff[(无符号字符)c]++;
freqdiff[c]++;
}
for(字符c:word2){
//或者,freqdiff[(无符号字符)c]-;
freqdiff[c]-;
}
用于(自动i:freqdiff){
//或者,i!=0
如果(1秒!=0){
返回false;
}
}
返回true;
}
带数组的布尔置换(常量std::string和word1,
const std::string和word2){
std::size\u t freqdiff[UCHAR\u MAX+1]={};
for(字符c:word1){
freqdiff[static_cast(c)]++;
}
for(字符c:word2){
freqdiff[静态(c)];
}
用于(std::size\u t i:freqdiff){
如果(i!=0){
返回false;
}
}
返回true;
}
}
int main(){
std::stringword1;
std::stringword2;
std::getline(std::cin,word1);
std::getline(std::cin,word2);
std::coutYep

#包括
#包括
#包括
名称空间{
布尔置换(常量std::string和word1,常量std::string和word2){
std::无序映射频率差;
//或者,std::size_t freqdiff[UCHAR_MAX+1]={};
for(字符c:word1){
//或者,freqdiff[(无符号字符)c]++;
freqdiff[c]++;
}
for(字符c:word2){
//或者,freqdiff[(无符号字符)c]-;
freqdiff[c]-;
}
用于(自动i:freqdiff){
//或者,i!=0
如果(1秒!=0){
返回false;
}
}
返回true;
}
带数组的布尔置换(常量std::string和word1,
const std::string和word2){
std::size\u t freqdiff[UCHAR\u MAX+1]={};
for(字符c:word1){
freqdiff[static_cast(c)]++;
}
for(字符c:word2){
freqdiff[静态(c)];
}
用于(std::size\u t i:freqdiff){
如果(i!=0){
返回false;
}
}
返回true;
}
}
int main(){
std::stringword1;
std::stringword2;
std::getline(std::cin,word1);
std::getline(std::cin,word2);

std::coutTL;DR我想测试解决方案(包括我自己的):David基于地图的答案表现得相当好(它更通用),他的基于数组的解决方案表现得非常好,我自己的解决方案只稍微快一点,但可读性也稍微差一点(可能不值得)

老实说,当我看到这一点时,我不敢相信大卫关于无序地图的回答可能具有最低的时间复杂度

我通常用C编写,所以我不知道C++对这些数据结构的优化,或者它们在实际生活中的表现如何。所以我决定测试它。 因此,我在i7上设置了一些测试,以测试各种解决方案的性能,并进行了一些轻微的调整()

我在1)2个排列和2)2个不同的单词上运行了100000次程序

结果如下:

PERM original
======================
PERMUTATIONS OF SAME WORD
real 104.73
user 104.61
sys 0.06

DIFFERENT WORDS
real 104.24
user 104.16
sys 0.02

PERM David map
======================
PERMUTATIONS OF SAME WORD
real 2.46
user 2.44
sys 0.00

DIFFERENT WORDS
real 2.45
user 2.42
sys 0.02

PERM David array
======================
PERMUTATIONS OF SAME WORD
real 0.15
user 0.14
sys 0.00

DIFFERENT WORDS
real 0.14
user 0.14
sys 0.00

PERM Me
======================
PERMUTATIONS OF SAME WORD
real 0.13
user 0.13
sys 0.00

DIFFERENT WORDS
real 0.14
user 0.12
sys 0.01

TL;DR我想测试解决方案(包括我自己的):David基于地图的答案表现不错(更通用),他的基于阵列的解决方案表现非常好,我自己的解决方案速度稍快,但可读性稍差(可能不值得)

老实说,当我看到这一点时,我不敢相信大卫关于无序地图的回答可能具有最低的时间复杂度

我通常用C编写,所以我不知道C++对这些数据结构的优化,或者它们在实际生活中的表现如何。所以我决定测试它。 因此,我在i7上设置了一些测试,以测试各种解决方案的性能,并进行了一些轻微的调整()

我在1)2个排列和2)2个不同的单词上运行了100000次程序

结果如下:

PERM original
======================
PERMUTATIONS OF SAME WORD
real 104.73
user 104.61
sys 0.06

DIFFERENT WORDS
real 104.24
user 104.16
sys 0.02

PERM David map
======================
PERMUTATIONS OF SAME WORD
real 2.46
user 2.44
sys 0.00

DIFFERENT WORDS
real 2.45
user 2.42
sys 0.02

PERM David array
======================
PERMUTATIONS OF SAME WORD
real 0.15
user 0.14
sys 0.00

DIFFERENT WORDS
real 0.14
user 0.14
sys 0.00

PERM Me
======================
PERMUTATIONS OF SAME WORD
real 0.13
user 0.13
sys 0.00

DIFFERENT WORDS
real 0.14
user 0.12
sys 0.01

为什么需要使用哈希表?对字符串中的字符进行排序,如果两个排序的字符串相同,则其中一个是另一个的排列。@latedeveloper排序将是n log(n),而这可以在线性时间内完成。我正在尝试更好地使用哈希表,所以我想尝试在这个程序中使用一个哈希表。为什么需要使用哈希表?对字符串中的字符进行排序,如果两个排序的字符串相同,则一个是另一个的排列。@latedeveloper排序将是n log(n),而这可以在线性时间内完成。我正在尝试更好地使用哈希表,所以我想尝试使用一个哈希表