后缀数组求不重复回文子串数目。注意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()

【HDOJ】3948 The Number of Palindromes的更多相关文章

  1. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  2. 【HDOJ】4162 Shape Number

    循环串的最小表示法. /* */ #include <iostream> #include <string> #include <map> #include < ...

  3. 【HDOJ】1018 Big Number

    数学题,还是使用log避免大数,但是不要忘记需要+1,因为0也是1位,log(100)= 2,但却是3位. #include <stdio.h> #include <math.h&g ...

  4. 【HDOJ】3006 The Number of set

    数据量这么小,果断状态压缩+dp. /* 3006 */ #include <iostream> #include <string> #include <map> ...

  5. 【HDOJ】5179 beautiful number

    DFS. /* 5179 */ #include <iostream> #include <algorithm> #include <map> #include & ...

  6. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  7. 【BZOJ4026】dC Loves Number Theory 分解质因数+主席树

    [BZOJ4026]dC Loves Number Theory Description  dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源.    给 ...

  8. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

随机推荐

  1. 【SQLite】使用事务处理带参数的插入

    using (SQLiteConnection conn = new SQLiteConnection(String.Format("Data Source={0};Pooling=true ...

  2. Kakfa揭秘 Day5 SocketServer下的NIO

    Kakfa揭秘 Day5 SocketServer下的NIO 整个Kafka底层都是基于NIO来进行开发的,这种消息机制可以达到弱耦合的效果,同时在磁盘有很多数据时,会非常的高效,在gc方面有非常大的 ...

  3. 《C和指针》读书笔记——第五章 操作符和表达式

    1.当/操作符的两个操作数都是整数时,它执行整除运算:其他都是执行浮点数除法. 2.逻辑移位:左边移入的位用0填充: 算数移位:左边移入的位用符号位填充: 3.位置1 :value |= 1<& ...

  4. dive into python 读笔(3)

    chapter 6 异常和文件处理: # 使用 try...except 来捕捉异常 # 使用 try...finally 来保护额外的资源 # 读取文件 # 在一个 for循环中一次赋多个值 # 使 ...

  5. crystal report format number

    ToText({#totalPrice}, 2,'.', ',')  &" €" http://crystaltricks.com/wordpress/?p=149

  6. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  7. REST Design Concerns

    Less Requests, More data; one of the core RESTful API design paradigms is the concept of less API re ...

  8. 1190: [HNOI2007]梦幻岛宝珠 - BZOJ

    Description 给你N颗宝石,每颗宝石都有重量和价值.要你从这些宝石中选取一些宝石,保证总重量不超过W,且总价值最大为,并输出最大的总价值. 数据范围:N<=100;W<=2^30 ...

  9. 10+ 最流行的 jQuery Tree 菜单插件

    jstree – jQuery Tree Plugin With HTML & JSON Data jstree is a lightweight and flexible jQuery pl ...

  10. js获取fck值的代码方法

    引入js文件 <script type="text/javascript" src="${basePath}/FCKeditor/fckeditor.js" ...