【HDOJ】4691 Front compression

后缀数组基础题目,dc3解。

 /* 4691 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int INF = 0x3f3f3f3f;
const int maxl = 1e5+;
const int maxn = 1e5+;
char s[maxl];
int nw;
int A[maxn], B[maxn];
int a[maxn*];
int height[maxn], rrank[maxn], sa[maxn*];
int wa[maxn], wb[maxn], wc[maxn], wv[maxn];
int dp[maxn][]; bool c0(int *r, int a, int b) {
return r[a]==r[b] && r[a+]==r[b+] && r[a+]==r[b+];
} bool c12(int k, int *r, int a, int b) {
if (k == )
return r[a]<r[b] || (r[a]==r[b] && c12(, r, a+, b+));
else
return r[a]<r[b] || (r[a]==r[b] && wv[a+]<wv[b+]);
} void sort(int *r, int *a, int *b, int n, int m) {
int i; for (i=; i<n; ++i) wv[i] = r[a[i]];
for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[wv[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) b[--wc[wv[i]]] = a[i];
} #define F(x) ((x)/3 + ((x)%3==1 ? 0:tb))
#define G(x) ((x)<tb ? (x)*3+1 : ((x)-tb)*3+2)
void dc3(int *r, int *sa, int n, int m) {
int i, j, *rn=r+n, *san=sa+n, ta=, tb=(n+)/, tbc=, p; r[n] = r[n+] = ;
for (i=; i<n; ++i) if (i%!=) wa[tbc++] = i;
sort(r+, wa, wb, tbc, m);
sort(r+, wb, wa, tbc, m);
sort(r, wa, wb, tbc, m);
for (p=, rn[F(wb[])]=, i=; i<tbc; ++i)
rn[F(wb[i])] = c0(r, wb[i-], wb[i]) ? p-:p++;
if (p < tbc)
dc3(rn, san, tbc, p);
else
for (i=; i<tbc; ++i) san[rn[i]] = i;
for (i=; i<tbc; ++i)
if (san[i] < tb)
wb[ta++] = san[i] * ;
if (n% == )
wb[ta++] = n - ;
sort(r, wb, wa, ta, m);
for (i=; i<tbc; ++i) wv[wb[i]=G(san[i])] = i;
for (i=,j=,p=; i<ta && j<tbc; ++p)
sa[p] = c12(wb[j]%, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
while (i < ta) sa[p++] = wa[i++];
while (j < tbc) sa[p++] = wb[j++];
} void calheight(int *r, int *sa, int n) {
int i, j, k = ; for (i=; i<=n; ++i) rrank[sa[i]] = i;
for (i=; i<n; height[rrank[i++]]=k)
for (k?k--:, j=sa[rrank[i]-]; r[j+k]==r[i+k]; ++k) ;
} void init_RMQ(int n) {
int i, j; for (i=; i<=n; ++i)
dp[i][] = height[i];
dp[][] = INF;
for (j=; (<<j)<=n; ++j)
for (i=; i+(<<j)-<=n; ++i)
dp[i][j] = min(dp[i][j-], dp[i+(<<(j-))][j-]);
} int RMQ(int l, int r) {
if (l > r)
swap(l, r); ++l;
int k = ; while (<<(k+) <= r-l+)
++k; return min(dp[l][k], dp[r-(<<k)+][k]);
} void printSa(int n) {
for (int i=; i<=n; ++i)
printf("%d ", sa[i]);
putchar('\n');
} void printRank(int n) {
for (int i=; i<=n; ++i)
printf("%d ", rrank[i]);
putchar('\n');
} void printHeight(int n) {
for (int i=; i<=n; ++i)
printf("%d ", height[i]);
putchar('\n');
} int getBit(int x) {
if (x == )
return ; int ret = ; while (x) {
++ret;
x /= ;
} return ret;
} void solve() {
int n = ; for (int i=; ; ++i) {
if (s[i] == '\0') {
n = i;
break;
}
a[i] = s[i] - 'a' + ;
}
a[n] = ; dc3(a, sa, n+, );
calheight(a, sa, n); #ifndef ONLINE_JUDGE
// printSa(n);
// printRank(n);
// printHeight(n);
#endif init_RMQ(n); __int64 ansa, ansb;
int pl, l, mnl, tmp; pl = B[] - A[];
ansa = pl;
ansb = pl + ; rep(i, , nw) {
l = B[i] - A[i];
mnl = min(l, pl);
if (A[i] == A[i-]) {
tmp = mnl;
} else {
tmp = RMQ(rrank[A[i-]], rrank[A[i]]);
if (tmp > mnl)
tmp = mnl;
}
#ifndef ONLINE_JUDGE
// printf("%d: tmp = %d\n", i, tmp);
#endif
ansa += l;
ansb += getBit(tmp) + + l - tmp;
pl = l;
} ansa += nw;
ansb += nw; printf("%I64d %I64d\n", ansa, ansb);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%s", s) != EOF) {
scanf("%d", &nw);
rep(i, , nw)
scanf("%d %d", &A[i], &B[i]);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据生成器。

 from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 20
bound = 10**3
lc = list(string.lowercase)
for tt in xrange(t):
length = randint(50, 105)
line = ""
for i in xrange(length):
idx = randint(0, 10)
line += lc[idx]
fout.write("%s\n" % (line))
n = randint(1, 20)
fout.write("%d\n" % (n))
for i in xrange(n):
a = randint(0, length-1)
b = randint(a+1, length)
fout.write("%d %d\n" % (a, b)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()
上一篇:【node】mongoose的基本使用


下一篇:POI读写大数据量excel,解决超过几万行而导致内存溢出的问题