Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
Algorithm 字符串解的秩_Algorithm_String Algorithm - Fatal编程技术网

Algorithm 字符串解的秩

Algorithm 字符串解的秩,algorithm,string-algorithm,Algorithm,String Algorithm,我在问一个问题,它要求你在按字典排序的排列中找到字符串的等级 O(N^2)是非常清楚的 一些网站也有。优化的部分基本上是预先填充计数数组,以便 计数[i]包含str中存在且小于i的字符的计数 我知道这会降低复杂性,但我不知道如何计算这个数组。这是执行此操作的函数(取自链接): //构造一个计数数组,其中每个索引上的值 //包含整个字符串中较小字符的计数 void populateAndIncreaseCount(int*count,char*str) { int i; 对于(i=0;str[i]

我在问一个问题,它要求你在按字典排序的排列中找到字符串的等级

O(N^2)是非常清楚的

一些网站也有。优化的部分基本上是预先填充
计数数组
,以便

计数[i]包含str中存在且小于i的字符的计数

我知道这会降低复杂性,但我不知道如何计算这个数组。这是执行此操作的函数(取自链接):

//构造一个计数数组,其中每个索引上的值
//包含整个字符串中较小字符的计数
void populateAndIncreaseCount(int*count,char*str)
{
int i;
对于(i=0;str[i];++i)
++计数[str[i]];
对于(i=1;i<256;++i)
计数[i]+=计数[i-1];
}
有人能对这个功能提供一个直观的解释吗?

这个解决方案正在做一个测试,然后对输出进行排序

bucket排序是
O(项目+可能的不同输入的数量)
,对于固定字母表,它可以作为
O(n)


然而,在实践中,UTF会产生相当大的字母表。因此,我建议改为快速排序。因为将快速排序分为
=
三个存储桶对于较大的字符集是有效的,但仍然可以利用较小的字符集。

再次阅读后理解。由于C++中语法错误而变得混乱。它实际上做了一件非常简单的事情(以下是java版本:

void populateAndIncreaseCount(int[] count, String str) {
    // count is initialized to zero for all indices
    for (int i = 0; i < str.length(); ++i) {
      count[str.charAt(i)]++;
    }

    for (int i = 1; i < 256; ++i)
      count[i] += count[i - 1];
  } 

该源代码中的实现非常糟糕-
int mul
将溢出超过12个字符的任何字符串。
void populateAndIncreaseCount(int[] count, String str) {
    // count is initialized to zero for all indices
    for (int i = 0; i < str.length(); ++i) {
      count[str.charAt(i)]++;
    }

    for (int i = 1; i < 256; ++i)
      count[i] += count[i - 1];
  } 
void updatecount (int* count, char ch)
{
    int i;
    for( i = ch; i < MAX_CHAR; ++i )
        --count[i];
}