5

これは私が念頭に置いていることですが、O(n^2) です:

例: 入力は「Thisisawesome」です。現在の文字を追加すると、古い対象セットが長くなり意味のあるものになるかどうかを確認する必要があります。しかし、どこまでバックアップする必要があるかを確認するには、先頭までたどる必要があります。例:「awe」と「some」は適切な言葉ですが、「awesome」はより大きな言葉になります。複雑さを改善する方法を提案してください。コードは次のとおりです。

void update(string in)
{
   int len= in.length();
   int DS[len];
   string word;
   for(int i=0; i<len; i++) DS[i]=0;

   for(int i=0; i<len; i++)
        for(int j=i+1; j<=len; j++)
        {
            word = in.substr(i,j-i);
            if(dict.find(word)!=dict.end())
                   DS[j-1] = (DS[j-1] > word.length()) ? DS[j-1] : word.length();   
         }
}
4

2 に答える 2

3

最初はO(n ^ 2)になるように見えますが、十分に大きいnと固定サイズの辞書の場合はO(n)のみであることが判明する動的計画法ソリューションがあります。

文字列を左から右に処理します。i番目の段階では、最初のi文字の解決策があるかどうかを判断する必要があります。これを解決するには、これらのi文字を2つのチャンクに分割するためのあらゆる可能な方法を検討してください。2番目のチャンクが単語であり、最初のチャンクを単語に分割できる場合、解決策があります。辞書で確認できる最初の要件。2番目の要件は、最初のj文字の答えが見つかったかどうかを確認することで確認できます。ここで、jは最初のチャンクの長さです。

これはO(n ^ 2)になります。これは、1,2,3、... nの長さのそれぞれについて、考えられるすべての分割を考慮するためです。ただし、辞書で最も長い単語が何であるかを知っている場合は、2番目のチャンクをこれより長くする分割を検討しても意味がないことがわかります。したがって、1,2,3 ... nの長さのそれぞれについて、最大でwの可能な分割を検討します。ここで、wは辞書内の最長の単語であり、コストはO(n)です。

于 2013-01-27T06:15:17.800 に答える
2

今日、ソリューションをコーディングしました。明日、Web サイトに掲載します。とにかく、方法は次のとおりです。

  1. トライで辞書を並べます。

    トライは、同じ文字で始まるすべての辞書単語を同時に照合できるため、複数の照合をすばやく行うのに役立ちます。

    (たとえば、"chairman" は "chair" と "chairman" をトライで一致させます。)

  2. ダイクストラ アルゴリズムを使用して、最適な一致を見つけます。

    (例えば、「議長」の場合、「c」を位置 0 と数える場合、0->5、0->8、1->5、2->5、5->8 の関係があります。これらの関係は形成されます。ダイクストラ アルゴリズムに最適なネットワークです。)

    (注: エッジの重みはどこにありますか? 次のポイントを参照してください。)

  3. 辞書の単語に重みを割り当てます。

    悪い一致を重み付けしないと、良い一致よりも重み付けされます。(例: "iamahero" は "i am a hero" ではなく "i ama hero" になります。)

    http://app.aspell.net/createにあるSCOWL ディクショナリは、さまざまなサイズのディクショナリを備えているため、目的を十分に果たします。これらのサイズ (10、20 など) は、計量に適しています)。

    何度か試した結果、「s」で終わる単語の重み付けを減らす必要があることがわかりました。そのため、「eyesandme」は「eyesandme」ではなく「eyes and me」になります。

段落をミリ秒単位で分割できました。このアルゴリズムは、分割される文字列の長さに比例して複雑になるため、メモリが十分にある限り、アルゴリズムは適切にスケーリングされます。

これがダンプです(自慢してすみません)。(選択された文章はウィキペディアの「小説」です。)

D:\GoogleDrive\programs\WordBreaker>"word breaker"<novelnospace.txt>output.txt

D:\GoogleDrive\programs\WordBreaker>type output.txt
Number of words after reading words-10.txt : 4101
Number of words after reading words-20.txt : 11329
Number of words after reading words-35.txt : 43292
Number of words after reading words-40.txt : 49406
Number of words after reading words-50.txt : 87966

Time elapsed in reading dictionary: 0.956782s

Enter the string to be broken into words:

Result:
a novel is along narrative normally in prose which describes fictional character
s and events usually in the form of a sequential story while i an watt in the ri
se of the novel 1957 suggests that the novel came into being in the early 18 th
century the genre has also been described as possessing a continuous and compreh
ensive history of about two thousand years with historical roots in classical gr
eece and rome medieval early modern romance and in the tradition of the novel la
the latter an italian word used to describe short stories supplied the present g
eneric english term in the 18 th century miguel de cervantes author of don quixo
te is frequently cited as the first significant europe an novelist of the modern
 era the first part of don quixote was published in 1605 while a more precise de
finition of the genre is difficult the main elements that critics discuss are ho
w the narrative and especially the plot is constructed the themes settings and c
haracterization how language is used and the way that plot character and setting
 relate to reality the romance is a related long prose narrative w alter scott d
efined it as a fictitious narrative in prose or verse the interest of which turn
s upon marvellous and uncommon incidents whereas in the novel the events are acc
ommodated to the ordinary train of human events and the modern state of society
however many romances including the historical romances of scott emily brontes w
u the ring heights and her man melvilles mo by dick are also frequently called n
ovels and scott describes romance as a kind red term romance as defined here sho
uld not be confused with the genre fiction love romance or romance novel other e
urope an languages do not distinguish between romance and novel a novel isle rom
 and err o ma nil roman z o

Time elapsed in splitting: 0.00495095s

D:\GoogleDrive\programs\WordBreaker>type novelnospace.txt
Anovelisalongnarrativenormallyinprosewhichdescribesfictionalcharactersandeventsu
suallyintheformofasequentialstoryWhileIanWattinTheRiseoftheNovel1957suggeststhat
thenovelcameintobeingintheearly18thcenturythegenrehasalsobeendescribedaspossessi
ngacontinuousandcomprehensivehistoryofabouttwothousandyearswithhistoricalrootsin
ClassicalGreeceandRomemedievalearlymodernromanceandinthetraditionofthenovellaThe
latteranItalianwordusedtodescribeshortstoriessuppliedthepresentgenericEnglishter
minthe18thcenturyMigueldeCervantesauthorofDonQuixoteisfrequentlycitedasthefirsts
ignificantEuropeannovelistofthemodernerathefirstpartofDonQuixotewaspublishedin16
05Whileamoreprecisedefinitionofthegenreisdifficultthemainelementsthatcriticsdisc
ussarehowthenarrativeandespeciallytheplotisconstructedthethemessettingsandcharac
terizationhowlanguageisusedandthewaythatplotcharacterandsettingrelatetorealityTh
eromanceisarelatedlongprosenarrativeWalterScottdefineditasafictitiousnarrativein
proseorversetheinterestofwhichturnsuponmarvellousanduncommonincidentswhereasinth
enoveltheeventsareaccommodatedtotheordinarytrainofhumaneventsandthemodernstateof
societyHowevermanyromancesincludingthehistoricalromancesofScottEmilyBrontesWuthe
ringHeightsandHermanMelvillesMobyDickarealsofrequentlycallednovelsandScottdescri
besromanceasakindredtermRomanceasdefinedhereshouldnotbeconfusedwiththegenreficti
onloveromanceorromancenovelOtherEuropeanlanguagesdonotdistinguishbetweenromancea
ndnovelanovelisleromanderRomanilromanzo
D:\GoogleDrive\programs\WordBreaker>
于 2015-06-30T16:44:39.187 に答える