Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
Beginners
(Moderators:
FPK
,
Tomas Hajny
) »
FreePascal: find all 3-symbol unique substring in string
Free Pascal
Website
Downloads
Wiki
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
laztoapk 0.9.0.43 is avai...
by
jmpessoa
[
Today
at 10:49:12 pm]
Validating user input
by
dukester
[
Today
at 10:43:46 pm]
about sqldb postgresql LO...
by
Red_prig
[
Today
at 09:00:58 pm]
increment filename if exi...
by
Josh
[
Today
at 08:34:47 pm]
Dark mode class...
by
Espectr0
[
Today
at 07:57:10 pm]
install problem in Pas2J...
by
arirod
[
Today
at 07:29:33 pm]
Command line compilation ...
by
marcov
[
Today
at 06:21:54 pm]
LAMW - "This app has a bu...
by
Agmcz
[
Today
at 06:15:40 pm]
Build lazarus and fpc in ...
by
MarkMLl
[
Today
at 04:32:59 pm]
weird sqlite error "datab...
by
Zvoni
[
Today
at 04:24:33 pm]
« previous
next »
Print
Pages:
1
[
2
]
Author
Topic: FreePascal: find all 3-symbol unique substring in string (Read 8459 times)
winni
Hero Member
Posts: 3045
Re: FreePascal: find all 3-symbol unique substring in string
«
Reply #15 on:
January 13, 2022, 11:55:41 pm »
Hi!
Keep it short and simple:
Code: Pascal
[Select]
[+]
[-]
program
CountPattern
;
{$mode objfpc}{$H+}
uses
Classes
;
var
sl
:
TStringList
;
s
,
pattern
:
string
;
i
:
integer
;
begin
sl
:
=
TStringList
.
create
;
sl
.
Sorted
:
=
true
;
sl
.
Duplicates
:
=
dupIgnore
;
write
(
'String: '
)
;
readln
(
s
)
;
for
i
:
=
1
to
length
(
s
)
-
2
do
begin
pattern
:
=
copy
(
s
,
i
,
3
)
;
sl
.
add
(
pattern
)
;
end
;
writeln
(
sl
.
count
,
' unique patterns'
)
;
sl
.
free
;
end
.
Winni
PS.: DupIgnore does not work without Sorted.
If it is not a bug then it is strange.
Yes I know - another Delphi compatible bastard .....
«
Last Edit: January 14, 2022, 12:01:45 am by winni
»
Logged
Thaddy
Hero Member
Posts: 11517
Re: FreePascal: find all 3-symbol unique substring in string
«
Reply #16 on:
January 14, 2022, 10:08:43 am »
Well, it would be implementable, but it would be really slow. I guess that is the reason.
Logged
Путин преступник. Россияне дезинформированы.
Thaddy
Hero Member
Posts: 11517
Re: FreePascal: find all 3-symbol unique substring in string
«
Reply #17 on:
January 14, 2022, 11:02:10 am »
Alternative that has just minor changes to your original code:
Use of
THashSet
- or if order is important
TSortedSet
- ,
For
instead of
foreach
and the addition uses
THashSet.add
.
Code: Pascal
[Select]
[+]
[-]
{$mode delphi}
uses
generics
.
collections
;
type
Mnoj
=
THashSet<string>
;
// or TSortedSet
var
S
:
string
;
i
:
integer
;
Mn3
:
Mnoj
;
begin
Mn3
:
=
Mnoj
.
Create
;
// or TSortedSet.Create
write
(
'String: '
)
;
readln
(
S
)
;
for
i
:
=
1
to
length
(
S
)
-
2
do
Mn3
.
Add
(
S
[
i
]
+
S
[
i
+
1
]
+
S
[
i
+
2
]
)
;
i
:
=
0
;
writeln
(
'Substring: '
)
;
for
S
in
Mn3
do
begin
Write
(
S
,
' '
)
;
i
:
=
i
+
1
;
end
;
writeln
;
writeln
(
'Sum='
,
i
)
;
Mn3
.
free
;
end
.
Voila. Works like a charm.
«
Last Edit: January 14, 2022, 11:05:57 am by Thaddy
»
Logged
Путин преступник. Россияне дезинформированы.
BobDog
Full Member
Posts: 248
Re: FreePascal: find all 3-symbol unique substring in string
«
Reply #18 on:
January 14, 2022, 04:58:41 pm »
This is about as short as I can do this:
Code: Pascal
[Select]
[+]
[-]
program
threes
;
var
s
:
ansistring
;
i
:
int32
;
begin
while
(
s<>
'q'
)
do
begin
write
(
'Enter string (or q to quit): '
)
;
read
(
s
)
;
for
i
:
=
1
to
length
(
s
)
-
2
do
if
(
pos
(
(
s
[
i
..
i
+
2
]
)
,
s
)
=
i
)
then
writeln
(
i
,
' '
,
s
[
i
..
i
+
2
]
)
;
readln
;
end
;
end
.
Logged
Print
Pages:
1
[
2
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
Beginners
(Moderators:
FPK
,
Tomas Hajny
) »
FreePascal: find all 3-symbol unique substring in string
TinyPortal
© 2005-2018