相关疑难解决方法(0)

搜索字符串集合的最快方法

问题:

我有一个大约120,000个用户(字符串)的文本文件,我想将其存储在一个集合中,然后再对该集合执行搜索.

每次用户更改a的文本时都会发生搜索方法TextBox,结果应该是包含文本的字符串TextBox.

我不必更改列表,只需将结果拉出来并将其放入ListBox.

到目前为止我尝试过的:

我尝试了两个不同的集合/容器,我正在从外部文本文件中转储字符串条目(当然是一次):

  1. List<string> allUsers;
  2. HashSet<string> allUsers;

使用以下LINQ查询:

allUsers.Where(item => item.Contains(textBox_search.Text)).ToList();

我的搜索事件(用户更改搜索文本时触发):

private void textBox_search_TextChanged(object sender, EventArgs e)
{
    if (textBox_search.Text.Length > 2)
    {
        listBox_choices.DataSource = allUsers.Where(item => item.Contains(textBox_search.Text)).ToList();
    }
    else
    {
        listBox_choices.DataSource = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

两者都给了我一个很差的响应时间(每次按键之间大约1-3秒).

题:

你认为我的瓶颈在哪里?我用过的系列?搜索方法?都?

如何获得更好的性能和更流畅的功能?

c# linq collections string-search winforms

79
推荐指数
10
解决办法
2万
查看次数

用字典解析字符串的算法

特定

  • 一个充满单词的字典,{in, july, den, dentist, best, ...}带有一些C++ API来访问它:boolean findWord(string word)或者string getNextWord(void)遍历它,

  • 一些没有空格的输入字符串,例如:bestdentistinjuly......

产量

  • best dentist in july is... (基本上用给定字典的空格分隔非空格字符串)

什么是解决它的最佳算法?

一个微妙但重要的问题是,是否有任何奇特的方法来解决无法到达的死胡同问题.例如,den并且dentist都是有效的词来剖析其余的字符串,其中一个可能只是一个死胡同.

对我来说,这似乎是一个贪婪的问题或动态编程可以解决的问题.

c++ algorithm parsing dictionary

11
推荐指数
1
解决办法
2143
查看次数

如何提高这种算法的性能?

我有一个100000对的文本文件:单词和频率.

test.in文件包含单词:

  • 1行 - 所有字频对的总数
  • 2行~100 001 - 字频对
  • 100 002行 - 用户输入字的总数
  • 从100 003到最后 - 用户输入的单词

我解析这个文件并把文字放进去

Dictionary<string,double> dictionary;
Run Code Online (Sandbox Code Playgroud)

我想在以下代码中执行一些搜索+命令逻辑:

for(int i=0;i<15000;i++)
{
    tempInputWord = //take data from file(or other sources)

    var adviceWords = dictionary
                .Where(p => p.Key.StartsWith(searchWord, StringComparison.Ordinal))
                .OrderByDescending(ks => ks.Value)
                .ThenBy(ks => ks.Key,StringComparer.Ordinal)
                .Take(10)
                .ToList();

    //some output
}
Run Code Online (Sandbox Code Playgroud)

问题:此代码必须在不到10秒的时间内运行.

在我的计算机(核心i5 2400,8gb RAM)上使用Parallel.For() - 大约91秒.

你能给我一些如何提高性能的建议吗?

更新:

万岁!我们做到了!谢谢@CodesInChaos,@ usr,@ T_D以及参与解决问题的所有人.

最终代码:

var kvList = dictionary.OrderBy(ks => ks.Key, StringComparer.Ordinal).ToList();

var strComparer = new MyStringComparer();
var intComparer = …
Run Code Online (Sandbox Code Playgroud)

.net c# parallel-processing performance dictionary

10
推荐指数
2
解决办法
1498
查看次数

如何从字符数组中查找单词?

解决这个问题的最佳方法是什么:

我有一组每组3-4个字符的数组,如下所示:

{p,     {a,    {t,    {m,
 q,      b,     u,     n,
 r,      c      v      o
 s      }      }      }
}
Run Code Online (Sandbox Code Playgroud)

我也有一系列字典单词.

找到字符数组是否可以组合形成字典单词之一的最佳/最快方法是什么?例如,上面的数组可以创建单词:

"pat","rat","at","to","bum"(lol)
但不是"nub"或"mat"

我应该循环通过字典来查看如果可以制作单词或从字母中获取所有组合,那么将它们与字典进行比较

algorithm dictionary

7
推荐指数
1
解决办法
5269
查看次数

优化数百万个char*到字符串转换

我有一个应用程序需要接受数百万个char*作为输入参数(通常字符串小于512个字符(在unicode中)),并将它们转换并存储为.net字符串.

它结果是我的应用程序性能的真正瓶颈.我想知道是否有一些设计模式或想法使其更有效.

有一个关键部分让我觉得它可以改进:有很多重复.假设有100万个对象进入,可能只有50个独特的char*模式.

为了记录,这里是我用于将char*转换为字符串的算法(此算法在C++中,但项目的其余部分在C#中)

String ^StringTools::MbCharToStr ( const char *Source ) 
{
   String ^str;

   if( (Source == NULL) || (Source[0] == '\0') )
   {
      str = gcnew String("");
   }
   else
   {
      // Find the number of UTF-16 characters needed to hold the
      // converted UTF-8 string, and allocate a buffer for them.
      const size_t max_strsize = 2048;

      int wstr_size = MultiByteToWideChar (CP_UTF8, 0L, Source, -1, NULL, 0);
      if (wstr_size < max_strsize)
      {
         // Save the malloc/free overhead if it's a …
Run Code Online (Sandbox Code Playgroud)

c# algorithm performance c++-cli string-conversion

7
推荐指数
1
解决办法
535
查看次数

生成字符串中子串的组合

我正在尝试为给定的单词生成所有可能的音节组合.识别什么是音节的过程在这里是不相关的,但它产生的所有组合都给我一个问题.我认为这可能是我想的几行递归(尽管其他任何方式都很好),但是我无法让它工作.有人可以帮忙吗?

    // how to test a syllable, just for the purpose of this example
    bool IsSyllable(string possibleSyllable) 
    {
        return Regex.IsMatch(possibleSyllable, "^(mis|und|un|der|er|stand)$");
    }

    List<string> BreakIntoSyllables(string word)
    {
       // the code here is what I'm trying to write 
       // if 'word' is "misunderstand" , I'd like this to return
       //  => {"mis","und","er","stand"},{ "mis","un","der","stand"}
       // and for any other combinations to be not included
    }
Run Code Online (Sandbox Code Playgroud)

c# string recursion combinations substring

5
推荐指数
1
解决办法
1130
查看次数