maspypy / library Goto Github PK
View Code? Open in Web Editor NEWcompro library
compro library
https://maspypy.github.io/library/string/longest_common_substring.hpp
#include "string/suffix_array.hpp"
template <typename STRING>
tuple<int, int, int, int> longest_common_substring(STRING& S, STRING& T) {
int dummy = max<int>(*max_element(all(S)), *max_element(all(T))) + 1;
STRING ST;
for (auto&& x: S) ST.push_back(x);
ST.push_back(dummy);
for (auto&& x: T) ST.push_back(x);
Suffix_Array X(ST);
auto& SA = X.SA;
auto& LCP = X.LCP;
tuple<int, int, int, int> res = {0, 0, 0, 0};
int n = 0;
FOR(i, len(ST) - 1) {
if ((SA[i] < len(S)) != (SA[i + 1] < len(S))) {
if (chmax(n, LCP[i])) {
// ここで、SA[i] は s の 部分文字列でない可能性があります。(swap必要)
//
int a = SA[i];
int b = SA[i + 1] - len(S) - 1;
res = {a, a + n, b, b + n};
}
}
}
return res;
}
少し調整しました (cppは苦手で擬似コード)
// swap追加
i1, i2 := sa[i], sa[i+1]
if i1 > i2 {
i1, i2 = i2, i1
}
a := i1;
b := i2 - len(s) - 1;
vals = [2,3,4]
target = 5
RE が出ました.
Lines 42 to 45 in 59f42d4
検証してみたら、「極小なものを返す」はできません。( 実際、O(n)では厳しいと思います )
n=7 m=7 edges= [[1,3],[3,5],[5,1],[0,2],[2,4],[4,6],[6,0]]
7 7
7 7
1 3
3 5
5 1
0 2
2 4
4 6
6 0
=>
出力:0、6、4、2 からなるサイクル
極小なものじゃないです。正解は 1、3、5 です。
もし何か問題があれば,教えて頂けると助かります。
These codes are not suitable for other implementations of FASTIO.
#ifdef FASTIO
template <int mod>
void rd(modint<mod> &x) {
fastio::rd(x.val);
assert(0 <= x.val && x.val < mod);
}
template <int mod>
void wt(modint<mod> x) {
fastio::wt(x.val);
}
#endif
There are many places that have this problem.This is an example.
To the people who implement read/print
with cin/cout
, I think this would be better.
#ifdef FASTIO
template <int mod>
void read(modint<mod> &x) {
read(x.val);
assert(0 <= x.val && x.val < mod);
}
template <int mod>
void print(modint<mod> x) {
print(x.val);
}
#endif
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.