【HDOJ】3948 The Number of Palindromes

时间:2023-12-13 14:55:20

后缀数组求不重复回文子串数目。注意dp数组。

 /* 3948 */
#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 = 2e5+;
const int maxn = 4e5+;
char s[maxl], ss[maxl];
int a[maxn];
int height[maxn], rrank[maxn], sa[maxn];
int wa[maxn], wb[maxn], wc[maxn], wv[maxn];
bool visit[maxn];
int dp[][maxn]; bool cmp(int *r, int a, int b, int l) {
return r[a]==r[b] && r[a+l]==r[b+l];
} void da(int *r, int *sa, int n, int m) {
int i, j, *x=wa, *y=wb, *t, p; for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[x[i]=r[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) sa[--wc[x[i]]]=i;
for (j=,p=; p<n; j*=, m=p) {
for (p=,i=n-j; i<n; ++i) y[p++] = i;
for (i=; i<n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i=; i<n; ++i) wv[i] = x[y[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) sa[--wc[wv[i]]] = y[i];
for (t=x,x=y,y=t, x[sa[]]=, p=,i=; i<n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p-:p++;
}
} 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[i+k]==r[j+k]; ++k) ;
} void printSa(int n) {
for (int i=; i<=n; ++i)
printf("%d ", sa[i]);
putchar('\n');
} void printHeight(int n) {
for (int i=; i<=n; ++i)
printf("%d ", height[i]);
putchar('\n');
} void printRank(int n) {
for (int i=; i<=n; ++i)
printf("%d ", rrank[i]);
putchar('\n');
} 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[j][i] = min(dp[j-][i], dp[j-][i+(<<(j-))]);
} int RMQ(int l, int r) {
if (l > r)
swap(l, r); ++l;
int k = ; while (<<(k+) <= r-l+)
++k; return min(dp[k][l], dp[k][r-(<<k)+]);
} void solve() {
int len = strlen(s);
int nn = len * + , n, nn2 = nn * ;
int l = ; rep(i, , len) {
a[l] = a[nn2-l] = ;
++l;
a[l] = a[nn2-l] = s[i]-'a'+;
++l;
}
a[l] = a[nn2-l] = ;
a[nn] = ;
a[nn2+] = ; n = nn2 + ;
da(a, sa, n+, );
calheight(a, sa, n); #ifndef ONLINE_JUDGE
// printSa(n);
// printHeight(n);
#endif init_RMQ(n); int ans = , mn = , tmp; memset(visit, false, sizeof(visit));
rep(i, , n+) {
mn = min(mn, height[i]);
if (visit[nn2-sa[i]]) {
tmp = RMQ(rrank[sa[i]], rrank[nn2-sa[i]]);
if (tmp > mn) {
ans += (tmp - mn) >> ;
mn = tmp;
}
} else {
visit[sa[i]] = true;
}
} printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
rep(tt, , t+) {
scanf("%s", s);
printf("Case #%d: ", tt);
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 =
bound = **
lc = list(string.lowercase)
fout.write("%d\n" % (t))
for tt in xrange(t):
length = randint(, )
line = ""
for i in xrange(length):
idx = randint(, )
line += lc[idx]
fout.write("%s\n" % (line)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()