-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.html
More file actions
191 lines (175 loc) · 5.88 KB
/
2.html
File metadata and controls
191 lines (175 loc) · 5.88 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>算法实验二</title>
<style>
* {
padding: 0;
margin: 0;
}
body {
width: 100%;
height: 100%;
background-image: url("image/jianyue.jpeg");
background-size: 100% 100%;
background-repeat: no-repeat;
background-attachment: fixed;
}
.first-container {
text-align: center;
padding-top: 150px;
font-size: large;
}
.first-container input {
margin-bottom: 20px;
border-radius: 5px;
font-size: 30px;
}
.first-container div {
padding-right: 300px;
color: white;
font-size: 25px;
padding-top: 20px;
}
.first-container button {
font-size: 25px;
border-radius: 5px;
background-color: green;
cursor: pointer;
}
.second-container {
text-align: center;
padding-top: 50px;
font-size: large;
}
.second-container input {
margin-bottom: 20px;
border-radius: 5px;
font-size: 30px;
}
.second-container div {
padding-right: 300px;
color: white;
font-size: 25px;
padding-top: 15px;
}
.second-container button {
font-size: 25px;
border-radius: 5px;
background-color: green;
cursor: pointer;
}
.second-container .ccc {
margin-left: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="first-container">
<input id="firstnum" type="text" placeholder="请在这里输入">
<button onclick="firstclick()">确定</button>
<div>最大子段和:<span id="res1"></span></div>
<div>besti: <span id="res2"></span></div>
<div>bestj: <span id="res3"></span></div>
</div>
<div class="second-container">
<input id="secondnum" type="text" placeholder="请在这里输入">
<input id="thirdnum" type="text" placeholder="请在这里输入">
<button onclick="secondclick()">确定</button>
<div>最长公共子序列: <span id="res4"> </span> </div>
<div>最长公共子序列长度: <span id="res5"> </span> </div>
</div>
</div>
<script>
var besti = 0;
var bestj = 0;
var maxSubArray = function (nums) {
let pre = 0, maxAns = nums[0];
nums.forEach((x) => {
pre = Math.max(pre + x, x);
if (pre == x) {
besti = nums.indexOf(x) + 1;
}
maxAns = Math.max(maxAns, pre);
if (maxAns == pre) {
bestj = nums.indexOf(x) + 1;
}
});
return maxAns;
};
function firstclick() {
var res1 = document.getElementById('res1');
var res2 = document.getElementById('res2');
var res3 = document.getElementById('res3');
var num = document.getElementById('firstnum');
nums = num.value;
nums = nums.split(" ");
var nums2 = [];
for (let i = 0; i < nums.length; i++) {
nums2.push(parseInt(nums[i]));
}
var result1 = maxSubArray(nums2);
if (result1 < 0) { res1.innerText = 0; }
else {
res1.innerText = result1;
res2.innerText = besti;
res3.innerText = bestj;
}
}
function printLCS(dp, str1, str2, i, j) {
if (i == 0 || j == 0) {
return "";
}
if (str1[i - 1] == str2[j - 1]) {
return printLCS(dp, str1, str2, i - 1, j - 1) + str1[i - 1];
} else {
if (dp[i][j - 1] > dp[i - 1][j]) {
return printLCS(dp, str1, str2, i, j - 1);
} else {
return printLCS(dp, str1, str2, i - 1, j);
}
}
}
var s;
function LCS(str1, str2) {
var m = str1.length
var n = str2.length
var dp = [new Array(n + 1).fill(0)] //第一行全是0
for (var i = 1; i <= m; i++) { //一共有m+1行
dp[i] = [0] //第一列全是0
for (var j = 1; j <= n; j++) {//一共有n+1列
if (str1[i - 1] === str2[j - 1]) {
//注意这里,str1的第一个字符是在第二列中,因此要减1,str2同理
dp[i][j] = dp[i - 1][j - 1] + 1 //对角+1
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1])
}
}
}
s = printLCS(dp, str1, str2, m, n)
return dp[m][n];
};
function secondclick() {
var k;
var res4 = document.getElementById('res4');
var res5 = document.getElementById('res5');
var num = document.getElementById('secondnum');
var thirdnum = document.getElementById('thirdnum');
nums1 = num.value;
nums2 = thirdnum.value;
nums3 = nums1.split(' ');
nums4 = nums2.split(' ');
console.log(nums3);
console.log(nums4);
var result2 = LCS(nums3, nums4);
s = s.split("").join(",");
res4.innerText = s;
res5.innerText = result2;
}
</script>
</body>
</html>