【HDOJ】3948 The Number of Palindromes
后缀数组求不重复回文子串数目。注意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的更多相关文章
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 【HDOJ】4162 Shape Number
循环串的最小表示法. /* */ #include <iostream> #include <string> #include <map> #include < ...
- 【HDOJ】1018 Big Number
数学题,还是使用log避免大数,但是不要忘记需要+1,因为0也是1位,log(100)= 2,但却是3位. #include <stdio.h> #include <math.h&g ...
- 【HDOJ】3006 The Number of set
数据量这么小,果断状态压缩+dp. /* 3006 */ #include <iostream> #include <string> #include <map> ...
- 【HDOJ】5179 beautiful number
DFS. /* 5179 */ #include <iostream> #include <algorithm> #include <map> #include & ...
- 【转】oracle数据库NUMBER数据类型
原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a) precision表示数字中的有效位;如果没有指定prec ...
- 【BZOJ4026】dC Loves Number Theory 分解质因数+主席树
[BZOJ4026]dC Loves Number Theory Description dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源. 给 ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
随机推荐
- HTTP 错误 404.2 解决方案
HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面 详细错误:HTTP 错误 404.2 - Not Found ...
- @Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别
ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别 对这四个的区别做一个总结,清理一下思路 ...
- c#怎么获取当前页面的url
Request.ApplicationPath: /testwebRequest.CurrentExecutionFilePath: /testweb/default.aspxRequest.File ...
- jQuery(function($){...})与(function($){...})(jQuery)知识点分享
写jQuery插件时一些经验分享一下. 一.推荐写法 jQuery(function($){ //coding }); 全写为 jQuery(document).ready(function($){ ...
- webpack减少打包后文件体积的几种方法
webpack 把我们所有的文件都打包成一个 JS 文件,这样即使你是小项目,打包后的文件也会非常大.下面就来讲下如何从多个方面进行优化. 去除不必要的插件 刚开始用 webpack 的时候,开发环境 ...
- 简单3D翻转
1.先上图~~~ 2.代码 html部分 <body> <div id="my3d"> <div id="box"> < ...
- jooml二次开发---添加文章组件
在写一个joomla组件的时候需要手动添加excel表格,并把表格当做文章的内容添加到前台文章中, 开始不知道怎么下手,索性先把一个基本的组件写出来,在joomla网站上测试是可以访问这个组件的,在p ...
- 1006. Sign In and Sign Out
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h& ...
- easy ui 下拉框绑定数据select控件
easy ui 中的下拉框控件叫做select,具体代码如下: html代码:①.这是一个公司等级的下拉框 <tr> <td>公司等级:</td> <td&g ...
- EXTJS 4.2 资料 控件之 xtype: "fieldcontainer",追加html
{ xtype: "fieldcontainer", layout: "hbox", items: [ { fieldLabel: '素材目录', name: ...