-
Notifications
You must be signed in to change notification settings - Fork 0
/
jielong.php
91 lines (72 loc) · 1.92 KB
/
jielong.php
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php header('Content-type:text');
require("PDO.class.php");
require('config.php');
$DB= new Db($DB_CONFIG['HOST'], $DB_CONFIG['DBNAME'], $DB_CONFIG['USERNAME'], $DB_CONFIG['PASSWORD']);
$chengyus=$DB->query("select chengyu,max_len from pre_org_chengyu");
$mark=array();
$head=array();
$tail=array();
$max_len=array();
foreach ($chengyus as $chengyu){
$max_len[$chengyu['chengyu']]=$chengyu['max_len'];
$mark[$chengyu['chengyu']]=0;
$head[$chengyu['chengyu']]=mb_substr($chengyu['chengyu'],0,1,'utf-8');
$tail[$chengyu['chengyu']]=mb_substr($chengyu['chengyu'],mb_strlen($chengyu['chengyu'],'utf-8')-1,1,'utf-8');
}
function array_shuffle($list)
{
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $list[$key];
return $random;
}
$start=$_GET['start'];
if(!array_key_exists($start,$mark)){
echo "抱歉,您输入的成语暂时未被收录。";
exit;
}
$current=array();
$best=array();
$search_time=0;
$max_search_time=6000;
function dfs($curr_cy){
global $current;
global $best;
global $mark;
global $head;
global $tail;
global $search_time;
global $max_search_time;
global $max_len;
$search_time=$search_time+1;
if($search_time>$max_search_time && $max_search_time!=0) return;
$current[]=$curr_cy;
if(count($current)>count($best)){
$best=$current;
}
$nexts=array_keys($head,$tail[$curr_cy]);
//print_r($nexts);
$nexts=array_shuffle($nexts);
//print_r($nexts);
foreach($nexts as $next){
if($mark[$next]==0 && count($current)+$max_len[$next]>count($best)){
$mark[$next]=1;
dfs($next);
array_pop($current);
$mark[$next]=0;
}
}
}
$mark[$start]=1;
dfs($start);
echo "(".count($best)."个成语)";
foreach($best as $cy){
echo '<a target="view_window" href="chengyu.html?query='.$cy.'">'.$cy.'</a> → ';
}
if($search_time>$max_search_time)
echo '......';
else echo '完';
?>