-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffixarray.cpp
278 lines (275 loc) · 6.96 KB
/
suffixarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Problem: POI2000 pow
* Author: Guo Jiabao
* Time: 2009.4.16 21:00
* State: Solved
* Memo: 多串最长公共子串 后缀数组 二分答案
*/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <vector>
#include <exception>
const int MAXL=0x1000f,MAXN=5002;
using namespace std;
class stkele{
public:
int height;
int loc;
bool ba[MAXN];
stkele(){
height = 0;
loc = 0;
memset(ba,0,sizeof(ba));
}
stkele(int h,int l,int idx1,int idx2){
height = h;
loc = l;
memset(ba,0,sizeof(ba));
ba[idx1]=true;
ba[idx2] = true;
}
};
class resele{
public:
int height;
int cover;
int loc;
resele(){
height = 0;
cover = 0;
loc = 0;
}
resele(int h,int c,int l){
height = h;
cover = c;
loc = l;
}
};
struct SuffixArray
{
struct RadixElement
{
int id,k[2];
}RE[MAXL],RT[MAXL];
int N,A[MAXL],SA[MAXL],Rank[MAXL],Height[MAXL],C[MAXL];
void RadixSort()
{
int i,y;
for (y=1;y>=0;y--)
{
memset(C,0,sizeof(C));
for (i=1;i<=N;i++) C[RE[i].k[y]]++;
for (i=1;i<MAXL;i++) C[i]+=C[i-1];
for (i=N;i>=1;i--) RT[C[RE[i].k[y]]--]=RE[i];
for (i=1;i<=N;i++) RE[i]=RT[i];
}
for (i=1;i<=N;i++)
{
Rank[ RE[i].id ]=Rank[ RE[i-1].id ];
if (RE[i].k[0]!=RE[i-1].k[0] || RE[i].k[1]!=RE[i-1].k[1])
Rank[ RE[i].id ]++;
}
}
void CalcSA()
{
int i,k;
RE[0].k[0]=-1;
for (i=1;i<=N;i++)
RE[i].id=i,RE[i].k[0]=A[i],RE[i].k[1]=0;
RadixSort();
for (k=1;k+1<=N;k*=2)
{
for (i=1;i<=N;i++)
RE[i].id=i,RE[i].k[0]=Rank[i],RE[i].k[1]=i+k<=N?Rank[i+k]:0;
RadixSort();
}
for (i=1;i<=N;i++)
SA[ Rank[i] ]=i;
}
void CalcHeight()
{
int i,k,h=0;
for (i=1;i<=N;i++)
{
if (Rank[i]==1)
h=0;
else
{
k=SA[Rank[i]-1];
if (--h<0) h=0;
for (;A[i+h]==A[k+h];h++);
}
Height[Rank[i]]=h;
}
}
}SA;
int N,Ans,Bel[MAXL];
char S[MAXL];
void init()
{
int i;
ifstream in("pow.in");
in>>N;
SA.N=0;
for (i=1;i<=N;i++)
{
int len = 0;
in>>len;
for(int p = 0;p<len;p++){
int tmp = 0;
in>>tmp;
// scanf("%d",&tmp);
SA.A[++SA.N] = tmp;
Bel[SA.N]=i;
}
if (i<N)
SA.A[++SA.N]=1000+i;
}
in.close();
}
bool check(int A)
{
int i,j,k;
bool ba[MAXN];
for (i=1;i<=SA.N;i++)
{
if (SA.Height[i]>=A)
{
for (j=i;SA.Height[j]>=A && j<=SA.N;j++);
j--;
memset(ba,0,sizeof(ba));
for (k=i-1;k<=j;k++)
ba[Bel[SA.SA[k]]]=true;
for (k=1;ba[k] && k<=N;k++);
if (k==N+1)
return true;
i=j;
}
}
return false;
}
void mysolve()
{
int i,j,k;
stack<stkele>stk;
std::vector<resele> v;
ofstream out("pow.out");
SA.CalcSA();
SA.CalcHeight();
// for(i = 1;i<=SA.N;i++)printf("%d ",SA.SA[i]);
// printf("\n");
for(i = 1;i<=SA.N;i++)
out<<SA.Rank[i]<<"\t";
out<<endl;
int maxlen = 0;
int maxcover = 0;
int maxidx = 0;
for (i=1;i<=SA.N;i++)
{
if(stk.empty()||SA.Height[i]>=stk.top().height)
stk.push(stkele(SA.Height[i],i-1,Bel[SA.SA[i]],Bel[SA.SA[i-1]]));
else{
bool ba[MAXN];
int cover = 0;
memset(ba,0,sizeof(ba));
while(!stk.empty()&&SA.Height[i]<stk.top().height){
stkele top(stk.top());
stk.pop();
for(int i =1;i<=N;i++){
if(ba[i]==false&&top.ba[i]==true){
cover+=1;
ba[i] = true;
}
}
// v.push_back(resele(top.height,cover,top.loc));
// printf("%d\t%d\t%d\n",top.height,cover,top.loc);
if(top.height*cover>maxlen*maxcover){
maxlen = top.height;
maxcover = cover;
maxidx = top.loc;
}
// out<<top.height<<'\t'<<cover<<'\t'<<top.loc<<endl;
}
ba[Bel[SA.SA[i]]] = true;
ba[Bel[SA.SA[i-1]]] = true;
stkele tmp(SA.Height[i],i-1,Bel[SA.SA[i]],Bel[SA.SA[i-1]]);
memcpy(&tmp.ba[0],&ba[0],sizeof(ba));
stk.push(tmp);
}
}
bool ba[MAXN];
int cover2 = 0;
memset(ba,0,sizeof(ba));
while(!stk.empty()){
stkele top2(stk.top());
stk.pop();
for(int i =1;i<=N;i++){
if(ba[i]==false&&top2.ba[i]==true){
cover2+=1;
ba[i] = true;
}
}
// v.push_back(resele(top.height,cover,top.loc));
if(top2.height*cover2>maxlen*maxcover){
maxlen = top2.height;
maxcover = cover2;
maxidx = top2.loc;
}
out<<maxlen<<'\t'<<maxcover<<'\t'<<maxidx<<endl;
}
/*
if (SA.Height[i]>=A)
{
for (j=i;SA.Height[j]>=A && j<=SA.N;j++);
j--;
memset(ba,0,sizeof(ba));
for (k=i-1;k<=j;k++)
ba[Bel[SA.SA[k]]]=true;
for (k=1;ba[k] && k<=N;k++);
if (k==N+1)
return true;
i=j;
}*/
out.close();
}
/*
void solve()
{
int a,b,m;
SA.CalcSA();
SA.CalcHeight();
a=0;b=SA.N;
while (a+1<b)
{
m=(a+b)/2;
if (check(m))
a=m;
else
b=m-1;
}
if (check(b))
a=b;
Ans=a;
}*/
extern "C"{
int solve(){
try{
init();
mysolve();
}
catch(exception e){
fstream out("pow.out");
out<<0<<endl;
out<<0<<'\t'<<0<<'\t'<<0<<endl;
out.close();
// cout<<e.what()<<endl;
// return 0;
}
return 1;
}
}