challenge 四字熟語パズルの作成

与えられた四字熟語のリストから下のように四角く配置することのできる熟語の組み合わせを探すプログラムを作成してください。

出力例:

無憂無風
礼  林
千  火
万水千山

知行合一
者  筆
不  勾
言語道断

四字熟語は左から右、上から下へ読むものとします。また右上隅の漢字と左下隅の漢字は異なるものでなければいけません。

四字熟語のデータは扱いやすい形(たとえばユニコード文字列のリスト)で与えられていると仮定して構いません。サンプルデータが必要であれば FOR Microsoft IME The四字熟語辞典(データ / 文書作成) にテキスト形式のデータが入っているのでそれを使えると思います。

問題の規模の参考までに、40行程度のPythonスクリプトでこのデータ(重複をのぞいて8312件)を処理してみたところ2.4GHzのCPUで13秒程度かかりました。結果は8133件出力されました。

Posted feedbacks - D

12109 個でした。

四字熟語データは重複を除いたものを改行区切りで UTF-8 で
words.txt に保存してあります。

Pentium4 3GHz、メモリ 2GB 下では 0.38 秒程度でした。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import std.stdio;
import std.file;
import std.string;
import std.utf;

void main(){
    wchar[][][wchar] hash;
    foreach(word; splitlines(cast(string)read("words.txt"))){
        wchar[] convedWord = word.toUTF16();
        hash[convedWord[0]] ~= convedWord;
    }
    bool[wchar[]] checked;
    int count;
    foreach(arr; hash){
        foreach(top; arr){
            if(top[0] in hash){
                foreach(left; hash[top[0]]){
                    if(!(left in checked) && top[3] in hash){
                        foreach(right; hash[top[3]]){
                            if(left[3] in hash && right != top && right != left){
                                foreach(bottom; hash[left[3]]){
                                    if(bottom[3] == right[3] && bottom[0] != top[3]
                                       && bottom != top && bottom != left && bottom != right){
                                        writefln(top);
                                        writefln(left[1], "  ", right[1]);
                                        writefln(left[2], "  ", right[2]);
                                        writefln(bottom);
                                        writefln("-------");
                                        count++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            checked[top] = true;
        }
    }
    writefln(count);
}

Index

Feed

Other

Link

Pathtraq

loading...