題意:約翰注意到奶牛產奶的之類是不斷變化的,雖然他不能**從當天到下一天的變化情況但是他知道變化是有規律的,牛奶的質量由乙個整數表示,範圍從0到1000000,現在給定乙個長度為n的序列,要求找到乙個最大子串行,該子串行重複出現至少k次,各個出現部分可有重疊,求最長的長度。簡單來說就是可重疊的k 次最長重複子串。
簡單來說就是求可重疊的k次最長重複子串
思路:這題首先先看資料的範圍,發現相差的有點大,所以我們先對資料進行離散化
離散化之後,其實這題和求不可重疊的最長重複子串的思想都是一樣的。
如果仔細體會了height的分組的思想,那麼不難知道這題其實就是要根據要求分組後該組的個數》=k
1 #include 2 #include 3 #include 4 #include5 #include 6 #include 7 #include 8 #include 910
#define inf 0x3f3f3f3f
11#define pii pair12
#define ll long long
13using
namespace
std;
14 typedef unsigned long
long
ull;
15const
int maxn = 2e6+6;16
17int
s[maxn];
18int
sa[maxn],t[maxn],t2[maxn],c[maxn];
19int
rank[maxn],height[maxn];
2021
void build_sa(int n,int
m)22
42for (i=0;i)
43 c[i] = 0;44
for (i=0;i)
45 c[x[y[i]]]++;
46for (i=1;i)
47 c[i] += c[i-1
];48
for (i=n-1;i>=0;i--)
49 sa[--c[x[y[i]]]] =y[i];
50swap(x,y);
51 p = 1
;52 x[sa[0]] = 0;53
for (i=1;i)
54 x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
55if (p>=n)
56break
;57 m =p;58}
59}6061
62void getheight(int
n)67
for (i=0;i)75}
7677
intn,k;
78int
b[maxn];
7980
void
discretization()
84 sort(b,b+n+1
);85
int size = unique(b,b+n+1)-b;
86for (int i=0;i<=n;i++)89}
9091
bool check(int
x)101
}102
return tot>=k;
103}
104105
intmain()
110 s[n] = 0
;111
discretization();
112 build_sa(n+1,n+1
);113
getheight(n);
114int l=0,r=n,mid;
115while (l<=r)
120else
123}
124 printf("
%d\n
",r);
125}
126return0;
127 }
1 #include 2 #include 3 #include 4 #include5 #include 6 #include 7 #include 8 #include 910
#define inf 0x3f3f3f3f
11#define pii pair12
#define ll long long
13using
namespace
std;
14 typedef unsigned long
long
ull;
15const
int maxn = 2e5 + 10;16
17int
wa[maxn], wb[maxn], wv[maxn], ws_[maxn];
18int
sta[maxn];
19int
cnt[maxn];
20void suffix(int *r, int *sa, int n, int
m)2142}
43int
rank[maxn], height[maxn], sa[maxn], r[maxn];
44int
s[maxn];
45int
b[maxn];
46int indexx[maxn],vis[200
];47
void calheight(int *r,int *sa,int
n)48
5455
bool check(int x,int n,int
k)61
else66}
67return tot>=k;68}
6970
void discretization(int
n)74 sort(b,b+n+1
);75
int size = unique(b,b+n+1)-b;
76for (int i=0;i<=n;i++)79}
8081
82int
main()
88 s[n] = 0;89
discretization(n);
90for (int i=0;i<=n;i++)
91 r[i] =s[i];
92 suffix(r,sa,n+1,n+1
);93
calheight(r,sa,n);
94int l=1,r=n,len=0;95
while (l<=r) else
101 r = mid-1
;102
}103 printf("
%d\n
",len);
104}
105return0;
106 }
最長可重疊的重複子串
題目大意 給定乙個字串,求它的最長可重疊的重複子串的長度 思路 求出height陣列之後,輸出最大值即可。因為最長可重疊的重複子串一定是在相鄰兩個字尾的最長公共字首,即height,而要求最大值,輸出height最大值即可 include include define max a,b a b?a b...
最長可重疊的重複子串(2)
勇幸 thinking 首先這是乙個單字串問題。子字串r 在字串l 中至少出現兩次,則稱r 是l 的重複子串。重複子串又分為可重疊重複子串和不可重疊重複子串,這裡只是簡單討論最長可重疊的重複子串,給出基本演算法和基於字尾陣列的演算法 關於字尾陣列,這裡也只是用最簡單的形式實現,對於字尾陣列的倍增演算...
求最長可重疊重複子串
問題描述 給定乙個字串,求出其可重疊最長重複子串 例如 abcdabcd 最長重複子串是 abcd,最長重複子串可以重疊 例如 abcdabcda,這時最長重複子串是 abcda,中間的 a 是被重疊的。直觀的解法是,首先檢測長度為 n 1 的字串情況,如果不存在重複則檢測 n 2,一直遞減下去,直...