【问题标题】:C++ Faster char array compareC++ 更快的字符数组比较
【发布时间】:2014-02-15 10:48:52
【问题描述】:

我想做的是找到一种更快的方法来执行相同的程序,但执行速度更快。

#include <cstdio>
#include <cstring>
int main ()

    {
        int br=0, c, n, b, e, i, q, g;
        char t[7000], a[7000];
        scanf("%d" ,&n);
        for (g=0; g<n; g++)  // number of test cases
        {
            scanf ("%7000s",&t);
            c=strlen(t);
            scanf ("%7000s",&a);
            b=strlen(a);
            for (i=0; i<b; i++)  // comparing them
            {
                for (q=0; q<c; q++)
                {
                    if (a[i]==t[q] && a[i]!='\0' && t[q]!='\0')
                    {
                    br++;a[i]='\0';t[q]='\0';
                    }
                }
            }
            printf("%d \n", br);
            br=0;
        }
        return 0;
    }  

程序本身会这样做:

第一个输入:测试用例的数量
第二:您必须为每个测试用例输入 A 数组和 B 数组
程序必须检查是否有任何来自 B 的常见字母与 A 匹配,如果有,则输出它们的数量。

Example input: 
2
qwerty
abc
abcde
bcex
Output: 
0
3

我需要的是让它运行得更快。 任何帮助都将不胜感激。 :)

【问题讨论】:

  • 只是为了兴趣..你有没有尝试从控制台输入 7000 个字符?
  • 不:D 为什么?这是否与问题有关?
  • 当您使用%s 扫描字符串时删除&amp;,例如scanf ("%7000s", &amp;t); 应该是 scanf ("%7000s", t);。顺便说一句,您的代码看起来更像是 C 而不是 C++。
  • 这不是 codechef 二月挑战的问题吗?比赛仍在进行中,您无法获得这样的解决方案..
  • 使用查找表,这将使您得到 O(N+M),其中 N 和 M 分别是两个字符串的长度。如果你能比这更快地得到它,你应该做我的工作。

标签: c++ arrays string performance compare


【解决方案1】:

最好对两个字符串中的每个字符进行哈希处理。每个字符对应一个 ASCII 值。将两个字符串的每个字符的计数存储在其他数组中。比较哈希数组。

【讨论】:

    【解决方案2】:

    将您的输入放在char 的两个排序容器中,例如deques。然后执行set intersection 并测量结果的大小。这应该会显着降低复杂性

    编辑

    使用用户输入修改已排序双端队列的示例

    void addChar2Deque(char newChar, std::deque<char> &sorted_cont) {
        sorted_cont.insert(std::lower_bound(sorted_cont.begin(), sorted_cont.end(), newChar), newChar);
    }
    

    如果您只能在预取数据中输入输入,那么您可以对该数据进行排序,例如对于 char 大小为 N 的数组

    std::sort(prefetched_data, prefetched_data+N);
    

    无论如何,您最终都会得到两个可以与std::set_intersection 进行比较的容器(deque 或 C 数组)

    std::vector<char> result;
    std::set_intersection(std::begin(cont1), std::end(cont1),
        std::begin(cont2), std::end(cont2), std::back_inserter(result));
    return result.size();
    

    【讨论】:

      【解决方案3】:

      为了符合 C 代码与 C++ 标头的口头禅,这是我提到的查找表方法。由此产生的复杂性是 O(N+M) 其中 N 和 M 是您各自字符串的长度。由于已成定局,每个字符串中的每个字符都必须至少访问一次(在这种情况下不超过一次),我有点自在地建议在算法方面如果不进入 asm 就很难做得更好袋装技巧。

      #include <cstdio>
      #include <cstdlib>
      #include <cstring>
      #include <climits>
      
      int main()
      {
          int n;
          if (scanf("%d", &n) != 1 || n < 0)
              return EXIT_FAILURE;
      
          while (n-- >0)
          {
              char a[7000], b[7000];
              if (scanf ("%7000s", a) == 1 && scanf ("%7000s", b) == 1)
              {
                  unsigned short table[1 << CHAR_BIT] = {0};
                  unsigned int answer = 0;
                  const char *p;
      
                  for (p=b; *p; ++table[(unsigned char)*p++]);
                  for (p=a; *p; ++p)
                  {
                      if (table[(unsigned char)*p])
                      {
                          --table[(unsigned char)*p];
                          ++answer;
                      }
                  }
      
                  printf("%u\n", answer);
              }
          }
      
          return EXIT_SUCCESS;
      }
      

      【讨论】:

      • 我将为您提供问题所在的 C++ 书中的输入/输出示例。 3 abcd qwe aaaa aa
      • @user3312862 我不需要它。正如我所说,这将是这两个答案之一。应该足够简单来澄清。
      • 当我删除该行时:table[(unsigned char)*p] = 0; 它对于 out 中的示例效果不佳...
      • 如果可以,我可以修复代码,但问题是我不知道您使用的表格,也无法理解您的部分代码。
      • 让我们考虑这个例子aabcacaa输出必须是3
      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 2011-06-21
      相关资源
      最近更新 更多