質問

どのようにしたら良いですかとって身体の安全を確保するための数倍の単語が表示されるtextfile?いすべての言葉です。

例えば、入力のようなもの:

"の人こんにちはになるのである。"

それぞれの"人こんにちはるボーイ"が発の1.

"のような洞2.

を考えているのを辞書で単語/発組が必要な場合はこちらかを実施する.リンク類似又は関連する問題を解決す。


編集:回避を圧延して、自分のハッシュテーブルその利用方法を習得glib.途中から優れたチュートリアルを散策を通じて同様の問題です。 http://bo.majewski.name/bluear/gnu/GLib/ch03s03.html

私awestruck数により異なるアプローチで、特にシンプルさと優雅さのRubyの実装です。

役に立ちましたか?

解決

あり、辞書で単語-熟ペアが、通常の方法で実施などの辞書を使うハッシュテーブルは、バイナリ検索。

このまま使用 trie (圧縮版 "パトリシア再生"/Radix trie)の複雑さが漸近的に最適なこの問題があって、実際にもより遅くなる良いハッシュテーブルの実装です。

[って思っているかどうかのハッシュテーブルはもうとしていよいよ配信の言葉を入力--例ハッシュテーブルのまま保管する必要が単語をそのハッシュバケット(防衝突、また多くの言葉を共通の接頭辞は、trieの方に共通の接頭辞で共有し、タ本体も、できるだけ一度だけ、それぞれがオーバーヘッドのすべてのポインタ...だってみて、思いものを比較できます。]

他のヒント

興味があるだけのために、ここにワードカウントの問題の簡単なRubyのソリューションです。それはちょうどより多くのコードで、基本的にCで同じアルゴリズムである必要があります。

h = Hash.new(0)
File.read("filename.txt").split.each do |w|
  h[w] += 1
end
p h

これはカウントしていますか?

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
    char buffer[2048];
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s file\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    snprintf(buffer, sizeof(buffer), "tr -cs '[a-z][A-Z]' '[\\n*]' < %s |"
                                     " sort | uniq -c | sort -n", argv[1]);
    return(system(buffer));
}

これは、基本的にシェルスクリプトとしてUnix上の単語をカウントする方法を説明する標準的なスクリプトをカプセル化します。

tr」コマンドは、改行にアルファベット文字ではなく、重複を絞り出すものを変換します。最初の「sort」一緒にグループの各単語のすべての出現。 「uniq -cは」言葉とそのカウントを印刷し、各単語の連続した出現回数をカウントします。 2番目の「sort」は増加繰り返しの順序でそれらを置きます。あなたは「tr」にオプションでDINKする必要があるかもしれません。それはシステムからシステムに最も安定コマンドではありません、それは日常私が手動バッシングを行う作るために管理しています。 / usr / binに/ TRを使用して、Solaris 10上に、上記のコードは、(それ自身のソースに)生成

   1
   1 A
   1 EXIT
   1 FAILURE
   1 Usage
   1 Z
   1 a
   1 c
   1 cs
   1 exit
   1 file
   1 fprintf
   1 if
   1 main
   1 return
   1 sizeof
   1 snprintf
   1 stderr
   1 stdio
   1 stdlib
   1 system
   1 tr
   1 uniq
   1 z
   2 argc
   2 char
   2 h
   2 include
   2 int
   2 s
   2 sort
   3 argv
   3 n
   4 buffer

あなたはハッシュテーブルを使用して、それがこれまでに発見された単語や回数を含む構造体へのハッシュ・テーブル・ポイントのすべてのエントリを持つことができます。

は、個々の単語については、これは大きな何かの一部である場合を除き、すべてのプログラムを記述する必要はありません

sed -e 's/[[:space:]]/\n/g' < file.txt | grep -c WORD

Perlでます:

my %wordcount = ();
while(<>){map {$wordcount{$_}++} (split /\s+/)}
print "$_ = $wordcount{$_}\n" foreach sort keys %wordcount;

と(ちょうど楽しみのために)Perlのゴルフでます:

my%w;                       
map{$w{$_}++}split/\s+/while(<>); 
print"$_=$w{$_}\n"foreach keys%w; 

警告 未審査コード:

#include <stdio.h>

struct LLNode
{
    LLNode* Next;    
    char*   Word;
    int     Count;
};

void PushWord(LLNode** list, const char* word)
{
    LLNode* node = NULL;
    unsigned int len = 0;
    if (*list == NULL) 
    {
        $list = new LLNode;
        $list = "\0";
    }
    node = *list;
    while ((node = node->Next) != NULL) // yes we are skipping the first node
    {
        if (!strcmp(node->Word, word))
        {
            node->Count++;
            break;
        }

        if (!node->Next)
        {
            LLNode* nnode = new LLNode;
            nnode->Count = 1;
            node->Next = nnode;
            len = strlen(word);
            node->Word = new char[len + 1];
            strcpy(node->Word, word);
            break;
        }
    }
}

void GetCounts(LLNode* list)
{
    if (!list)
        return;
    LLNode* node = list;
    while ((node = node->Next) != NULL) // yes we are skipping the first node
    {
        printf("Word: %s, Count: %i", node->Word, node->Count);
    }
}

void PushWords(LLNode** list, const char* words)
{
    char ch = '\0';
    unsigned int len = strlen(words);
    char buff[len]; // to be sure we have no buffer ovverunes. May consume too much memery for your application though.
    int index = 0;
    for (unsigned int i = 0; i < len; i++)
    {
        ch = words[i];
        if (index > 0 && ch == ' ')
        {
            ch[index + 1] = '\0';
            PushWords(list, buff);
            index = 0;
        }
        else if (ch != ' ')
        {
            ch[index++] = ch;
        }
    }

    if (index > 0 && ch == ' ')
    {
        ch[index + 1] = '\0';
        PushWords(list, buff);
        index = 0;
    }
}

int main()
{
    LLNode* list = NULL;
    PushWords(&list, "Hello world this is a hello world test bla");
    GetCount(list);
    // release out memery here
}

私の記事は英語で書かれた記事をたった今でprobolyポワークでは一般的なアイデアです。

別の荒案はこのC++(注std::マップは良いの検索回):

#include <iostream>
#include <string>
#include <map>

using namespace std;

typedef map<string, int> CountMap;

void PushWords(CountMap& list, const char* words)
{
    char ch = '\0';
    unsigned int len = strlen(words);
    string str;
    int index = 0;
    for (unsigned int i = 0; i < len; i++)
    {
        ch = words[i];
        if (index > 0 && ch == ' ')
        {
            list[str] = list[str] + 1;
            index = 0;
        }
        else if (ch != ' ')
        {
            str += ch;
            index++;
        }
    }

    if (index > 0 && ch == ' ')
    {
        list[str] = list[str] + 1;
    }
}

void PrintCount(CountMap& list)
{
    CountMap::iterator iter = list.begin(), end = list.end();
    for (; iter != end; ++iter)
    {
        cout << (*iter).first << " : " << (*iter).second;
    }
}


int main()
{
    CountMap map;
    PushWords(map, "Hello world this is a hello world test bla");
    PrintCount(map);
}
#include <conio.h>
#include <iostream.h>
#include <fstream.h>
#include <cstdlib>

struct stdt
{
       char name[20] ;
       int id ;

}; //std

int main()
{
      stdt boy ;
      int a = 0 ;
      ofstream TextFile ;
      cout << "Begin File Creation \n" ;
      TextFile.open("F:\\C++ Book Chapter Program\\Ch  7\\File.txt" );
      if ( !TextFile)
      {
           cerr <<"Erro 100 Openoing File.DAT" ;
           exit(100);     
      }//end if
      while ( a < 3 )
      {
            TextFile.write( (char*) &boy , sizeof (boy) ) ;
            cout << "\nEnter Name : " ;
            cin  >> boy.name;
            cout << "\nEnter ID : " ;
            cin  >> boy.id ;
            a++;
      }//end while

      TextFile.close();
      cout << "\nEnd File Creation" ;

      ifstream TextFile1 ;
      TextFile1.open("F:\\C++ Book Chapter Program\\Ch  7\\File.txt" );
      while ( TextFile1.read( (char*) &boy , sizeof (boy) ) )
      {
            cout << "\nEnter Name : " << boy.name; 
            cout << "\nEnter ID : " << boy.id ;


      }// end While

      getch();
      return 0 ;
}//end main
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top