Tükörszó probléma megoldása
Pédaprogram
A megoldás matematikai modellje
Megoldás rekurzió-memorizálás módszerével
let o = {
mi(i=0,j=this.s.length-1) {
!this.t ? this.t = Array(this.s.length)
.fill()
.map(v=>Array(this.s.length)
.fill(0))
: null
return this.t[i][j] ? this.t[i][j] : i>=j ? this.t[i][j] = 1 :
this.s[i]===this.s[j] ? this.t[i][j] = this.mi(i+1,j-1) :
this.t[i][j] = Math.min(this.mi(i+1,j),this.mi(i,j-1))+1
},
mo(i=0,j=this.s.length-1) {
let s=this.s.split('')
do this.t[i][j-1]+1===this.t[i][j] ? s[j--]='.' :
this.t[i+1][j]+1===this.t[i][j] ? s[i++]='.' : ( i++, j-- )
while (this.t[i][j]-1)
return s.join('')
}
}
o.s = 'pelda'
console.log(`
${ o.x = o.mi()-1 } karakter törlésével a tükörszó:
${ o.x?o.mo():o.s }
`)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
VueComponent forrás
<template>
<div id=cont>
<div id=app>
<input v-model="s"
@keyup="szamol()"
placeholder="Szó / Szöveg"/>
<hr>
<table v-if="mi">
<tr><td class="szoveg"
:colspan="s.length"
title="A törlendő karakterek piros mezőben, az előállított tükörszó zöldben."
><pre>Törlendő</pre></td></tr>
<tr>
<td class="eredm" :colspan="s.length">
<pre>legalább {{ mi }} karakter:</pre>
</td>
</tr>
<tr>
<td :class="el=='.'?'z':'x'"
v-html="el==='.'?s[i]:el"
v-for="(el,i) in mo"/>
</tr>
<tr>
<td class="szoveg" :colspan="s.length">
<pre>t[i,j]</pre>
</td>
</tr>
<tr v-for="(row,j) in t">
<td :class="cell?'x':'o'"
:key="`${i}-${j}`"
v-for="(cell,i) in row">
{{ cell?cell-1:'' }}
</td>
</tr>
</table>
<div v-else>Ez tükörszó</div>
</div>
<hr>
<a href="/~tnemeth/examples/algoexamples/Tukorszo_forras.html">
A megoldás matematikai modelleje és forráskódja
</a>
</div>
</template>
<script>
let o = {
mi(i=0,j=this.s.length-1) {
return this.t[i][j] ? this.t[i][j] : i>=j ? this.t[i][j] = 1 :
this.s[i]===this.s[j] ? this.t[i][j] = this.mi(i+1,j-1) :
this.t[i][j] = Math.min(this.mi(i+1,j),this.mi(i,j-1))+1
},
mo(i=0,j=this.s.length-1) {
let s=this.s.split('')
do this.t[i][j-1]+1===this.t[i][j] ? s[j--]='.' :
this.t[i+1][j]+1===this.t[i][j] ? s[i++]='.' : ( i++, j-- )
while (this.t[i][j]-1)
return s.join('')
}
}
export default {
name: 'tsz',
data: () => ({
s: "",
mi: 0,
mo: "",
t: []
}),
methods: {
szamol() {
this.s=this.s.toLocaleLowerCase()
if (this.s.length) {
o.t = Array(this.s.length)
.fill()
.map(v=>Array(this.s.length).fill(0))
o.s=this.s
this.mi=o.mi()-1
this.mo=this.mi?o.mo():o.s
this.t=o.t
}
}
}
}
</script>
<style scoped>
div#app {
font-size: 20px;
}
input {
background-color: rgb(196, 232, 238);
width: 327px;
font-size: 20px;
padding: 5px;
border-radius: 5px;
}
table {
padding: 0px;
margin: 0px;
border-collapse: separate;
border-spacing: 3px;
display: table;
}
td {
text-align: center;
font-size: 15px;
padding: 2px;
width: 17px;
height: 17px;
box-shadow: 1px 1px 2px gray;
border-radius: 6px;
}
td.z {
background-color: rgb(235, 88, 98);
font-weight: bold;
color:snow;
}
td.x {
background-color: rgb(217, 252, 233);
font-weight: bold;
}
td.szoveg {
user-select: none;
cursor: default;
text-align: center;
background-color: rgb(251, 255, 212);
font-size: 18px;
width: 320px;
}
td.eredm {
font-weight: bold;
font-size: 18px;
padding: 4px;
background-color: rgb(169, 241, 249);
}
pre {
margin: 2px ;
padding: 0 ;
background-color: rgb(231, 235, 202);
box-shadow: 0 0 2px black;
}
</style>
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
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