Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
PragmaticProgramming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Regler
PragmaticProgramming
Commits
96a1af5d
Commit
96a1af5d
authored
Dec 7, 2023
by
Max Nilsson
Browse files
Options
Downloads
Patches
Plain Diff
String stuff
parent
9914f6f2
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Code/suffix_array_test.cpp
+81
-0
81 additions, 0 deletions
Code/suffix_array_test.cpp
Code/trie.cpp
+37
-0
37 additions, 0 deletions
Code/trie.cpp
Slides/Pragmatic_Programming_Week7.pdf
+0
-0
0 additions, 0 deletions
Slides/Pragmatic_Programming_Week7.pdf
with
118 additions
and
0 deletions
Code/suffix_array_test.cpp
0 → 100644
+
81
−
0
View file @
96a1af5d
#include
<bits/stdc++.h>
using
namespace
std
;
#define Mod(x,y) (((x)%(y)+(y))%(y))
#define rep(i, a, b) for(ll (i) = (a); (i) < (b); ++(i))
#define all(x) begin(x), end(x)
#define pb push_back
#define gcd __gcd
#define sz(x) (ll)(x.size())
typedef
long
long
ll
;
typedef
unsigned
long
long
ull
;
typedef
pair
<
ll
,
ll
>
pii
;
typedef
vector
<
ll
>
vi
;
typedef
vector
<
pii
>
vii
;
const
bool
debug
=
false
;
struct
SuffixArray
{
vi
sa
,
lcp
;
SuffixArray
(
string
&
s
,
int
lim
=
256
)
{
// or basic_string<int>
int
n
=
sz
(
s
)
+
1
,
k
=
0
,
a
,
b
;
vi
x
(
all
(
s
)
+
1
),
y
(
n
),
ws
(
max
(
n
,
lim
)),
rank
(
n
);
sa
=
lcp
=
y
,
iota
(
all
(
sa
),
0
);
for
(
int
j
=
0
,
p
=
0
;
p
<
n
;
j
=
max
(
1
,
j
*
2
),
lim
=
p
)
{
p
=
j
,
iota
(
all
(
y
),
n
-
j
);
rep
(
i
,
0
,
n
)
if
(
sa
[
i
]
>=
j
)
y
[
p
++
]
=
sa
[
i
]
-
j
;
fill
(
all
(
ws
),
0
);
rep
(
i
,
0
,
n
)
ws
[
x
[
i
]]
++
;
rep
(
i
,
1
,
lim
)
ws
[
i
]
+=
ws
[
i
-
1
];
for
(
int
i
=
n
;
i
--
;)
sa
[
--
ws
[
x
[
y
[
i
]]]]
=
y
[
i
];
swap
(
x
,
y
),
p
=
1
,
x
[
sa
[
0
]]
=
0
;
rep
(
i
,
1
,
n
)
a
=
sa
[
i
-
1
],
b
=
sa
[
i
],
x
[
b
]
=
(
y
[
a
]
==
y
[
b
]
&&
y
[
a
+
j
]
==
y
[
b
+
j
])
?
p
-
1
:
p
++
;
}
rep
(
i
,
1
,
n
)
rank
[
sa
[
i
]]
=
i
;
for
(
int
i
=
0
,
j
;
i
<
n
-
1
;
lcp
[
rank
[
i
++
]]
=
k
)
for
(
k
&&
k
--
,
j
=
sa
[
rank
[
i
]
-
1
];
s
[
i
+
k
]
==
s
[
j
+
k
];
k
++
);
}
};
string
ob_la_di
=
"DesmondhasabarrowinthemarketplaceMollyisthesingerinabandDesmondsaystoMolly,
\"
Girl,Ilikeyourface
\"
AndMollysaysthisasshetakeshimbythehand"
;
string
longest_repeating_substring
(
string
s
)
{
struct
SuffixArray
suffix_array
(
s
);
int
i_max
,
mx
=
-
1
;
int
ind
=
0
;
for
(
int
i
:
suffix_array
.
sa
)
{
if
(
suffix_array
.
lcp
[
ind
]
>
mx
)
i_max
=
i
,
mx
=
suffix_array
.
lcp
[
ind
];
ind
++
;
}
return
s
.
substr
(
i_max
,
mx
);
}
void
solve
()
{
struct
SuffixArray
suffix_array
(
ob_la_di
);
cout
<<
string
(
ob_la_di
.
size
()
+
20
,
'-'
)
<<
'\n'
;
cout
<<
"Printing the suffix array with associated lcp!
\n
"
;
cout
<<
"Pattern
\n
"
;
cout
<<
"lcp
\t
| suffix
\n
"
;
int
ind
=
0
;
for
(
int
i
:
suffix_array
.
sa
)
{
cout
<<
suffix_array
.
lcp
[
ind
]
<<
"
\t
| "
<<
ob_la_di
.
substr
(
i
)
<<
endl
;
ind
++
;
}
cout
<<
string
(
ob_la_di
.
size
()
+
20
,
'-'
)
<<
'\n'
;
cout
<<
"longest repeating substring is:
\n
"
;
cout
<<
longest_repeating_substring
(
ob_la_di
)
<<
endl
;
}
int
main
()
{
ios
::
sync_with_stdio
(
0
);
cin
.
tie
(
0
);
cout
<<
setprecision
(
15
)
<<
fixed
;
solve
();
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Code/trie.cpp
0 → 100644
+
37
−
0
View file @
96a1af5d
const
int
SIGMA
=
26
;
typedef
struct
TrieNode
{
struct
TrieNode
*
children
[
SIGMA
];
bool
isEndOfWord
;
}
TrieNode
;
TrieNode
*
getNode
()
{
TrieNode
*
node
=
new
TrieNode
;
node
->
isEndOfWord
=
false
;
rep
(
i
,
0
,
SIGMA
)
node
->
children
[
i
]
=
NULL
;
return
node
;
}
void
insert
(
TrieNode
*
root
,
string
key
)
{
TrieNode
*
node
=
root
;
rep
(
i
,
0
,
key
.
size
())
{
int
ind
=
key
[
i
]
-
'a'
;
if
(
!
node
->
children
[
ind
])
node
->
children
[
ind
]
=
getNode
();
node
=
node
->
children
[
ind
];
}
node
->
isEndOfWord
=
true
;
}
bool
search
(
TrieNode
*
root
,
string
key
)
{
TrieNode
*
node
=
root
;
rep
(
i
,
0
,
key
.
size
())
{
int
ind
=
key
[
i
]
-
'a'
;
if
(
!
node
->
children
[
ind
])
return
false
;
node
=
node
->
children
[
ind
];
}
return
node
->
isEndOfWord
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Slides/Pragmatic_Programming_Week7.pdf
0 → 100644
+
0
−
0
View file @
96a1af5d
File added
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment