Skip to content

Commit 74aca69

Browse files
Update string-algorithms.md
1 parent faf4014 commit 74aca69

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

‎string-algorithms.md‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,32 @@ int numberOfDiffSubstrings(vector<int> &lcp, string &s)
655655
}
656656
```
657657
658-
### Longest Common Substring
658+
### (LCS) Longest Common Substring
659+
660+
https://codeforces.com/edu/course/2/lesson/2/5
661+
659662
```c++
663+
/*
664+
Can be done using DP there will be two states pointer i and pointer j and each transition will be constant checking if pointer are equal or not.
665+
This will be O(N*M)
666+
667+
668+
Idea is for a given s and t string: aabba and baba we append like aabba#baba now create suffix array and lcp array
669+
a 2
670+
a#baba 1
671+
aabba#baba 1
672+
aba 2
673+
abba#baba 1
674+
ba 2
675+
ba#baba 1
676+
baba 2
677+
bba#baba 1
678+
679+
1/2 represents where the string is comming from 1 means its string s and 2 means its string 2.
680+
This can be determined is suffix array ith element index is greater than s size
681+
If both i and i+1 are from same string and lcp (i.e. common between i and i+1 here in code indexing is one minus because 0th element lcp doesn't exist)
682+
LCP symbolises a potential Longest common substring.
683+
*/
660684
string s, t; cin >> s >> t;
661685
string str = s + '#' + t;
662686
auto sa = constructSuffixArray(str);

0 commit comments

Comments
(0)