[hdu3336]kmp(后缀数组)
题意:求字符串s的所有前缀出现次数之和。
http://www.cnblogs.com/jklongint/p/4446117.html
思路:用kmp做,简单且效率高。以前缀结尾的位置分类,令dp[i]为以结尾位置在i的前缀数量,那么dp[i] = cnt(j)(j~i是前缀),而由kmp的next函数的转移性质,可得如下递推方程:dp[i] = dp[next[i]] + 1,把这个递推式不断展开,也就是i = next[i]不断迭代,那么+1的个数就是dp[i] = cnt(j)(j~i是前缀)这个里面的cnt(j)了。这就是next数组的性质,实在巧妙!
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl typedef double db;
typedef long long LL;
typedef pair<int, int> pii;
typedef multiset<int> msi;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 2e5 + ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } struct KMP {
int next[];
void GetNext(char s[]) {
mem0(next);
next[] = next[] = ;
for(int i = ; s[i]; i++) {
int j = next[i];
while(j && s[i] != s[j]) j = next[j];
next[i + ] = s[j] == s[i]? j + : ;
}
}
}; KMP kmp;
int dp[maxn];
char s[maxn]; int main() {
//freopen("in.txt", "r", stdin);
int T, n;
cin >> T;
while (T--) {
scanf("%d%s", &n, s);
kmp.GetNext(s);
mem0(dp);
int ans = n;
rep_up1(i, n) {
if (kmp.next[i] == ) continue;
dp[i] = dp[kmp.next[i]] + ;
dp[i] %= md;
ans += dp[i];
ans %= md;
}
cout << ans << endl;
}
return ;
}
[hdu3336]kmp(后缀数组)的更多相关文章
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- 【HDU - 5442】Favorite Donut 【最大表示法+KMP/后缀数组】
题意 给出一个长度为n的环状由小写字母组成的序列,请找出从何处断开,顺时针还是逆时针,使得字典序最大.如果两个字符串的字典序一样大,那么它会选择下下标最小的那个.如果某个点顺时针逆时针产生的字典序大小 ...
- POJ2406Power Strings (最小循环节)(KMP||后缀数组)
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- luogu 2463 [SDOI2008]Sandy的卡片 kmp || 后缀数组 n个串的最长公共子串
题目链接 Description 给出\(n\)个序列.找出这\(n\)个序列的最长相同子串. 在这里,相同定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串. 思路 参考:hzwe ...
- ZOJ1905Power Strings (KMP||后缀数组+RMQ求循环节)
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- POJ3080方法很多(暴力,KMP,后缀数组,DP)
题意: 给n个串(n>=2&&n<=10),每个串长度都是60,然后问所有串的最长公共子串,如果答案不唯一输出字典序最小的. 思路:直接暴力,枚举+KMP,后缀 ...
- LightOJ 1428 Melody Comparison (KMP + 后缀数组)
题意:给定两个串A,B,问你A有多少不同的子串,并且不包含B. 析:首先A有多少个不同的子串,可以用后缀数组来解决,也就是 n - sa[i] - h[i] + 1.但是要是不包含B,可以先预处理A和 ...
随机推荐
- DeepinV20系统文件管理器右键发送至为知笔记
1. 创作背景 昨天在深度系统上做了一个打开文件管理器选择文件右键发送文本至博客园的插件. 这个插件对于我自己来说是及其方便的东西,平时的学习积累,工作经验或者生活感悟,随手记下之后,就能够轻松发送出 ...
- Linux提权-suid提权
0x1 suid概念 通俗理解为其他用户执行这个程序是可以用该程序所有者/组的权限 0x2 suid提权 简单理解为,一个文件有s标志(权限中包含s),并且对应的是root权限,那么当运行这个程序时就 ...
- Des对称可逆加密
/// <summary> /// DES AES Blowfish /// 对称加密算法的优点是速度快, /// 缺点是密钥管理不方便,要求共享密钥. /// 可逆对称加密 密钥长 ...
- Youtube推荐算法的前世今生
第一阶段,基于User-Video图游历算法,2008年[1]. 在这个阶段,YouTube认为应该给用户推荐曾经观看过视频的同类视频,或者说拥有同一标签的视频.然而此时,YouTube的视频已是数千 ...
- tp5.1 模型 where多条件查询 like 查询
来源:https://blog.csdn.net/qq_41241684/article/details/87866416 所以我改成这样: $paperTypeModel = new PaperTy ...
- tp5中“前置操作”和“钩子函数”的区别
1.实行顺序: 以下都是标为删除前的操作: 点击删除 -> 前置操作 -> 删除方法(用模型删除) -> 触发钩子函数(删除) -> 删除成功 2.传入的参数: ...
- python学习22之函数式编程
'''''''''1.高阶函数:将函数作为参数传递到另一个函数中,作为这个函数的参数,这样的方式叫高阶函数(1)map:两个参数,一个是函数,一个是iterator,将函数依次作用于Iterator中 ...
- Ubuntu忘记超级用户root密码,重新设置密码 转载
原文链接:https://blog.csdn.net/weixin_37909391/article/details/80691601 Ubuntu版本:Ubuntu 16.04.3 LTS 1启动系 ...
- Linux网络服务第五章NFS共享服务
1.笔记 NFS一般用在局域网中,网络文件系统c/s格式 服务端s:设置一个共享目录 客户端c:挂载使用这个共享目录 rpc:111远程过程调用机制 Showmount -e:查看共享目录信息 def ...
- Pytorch中自定义神经网络卷积核权重
1. 自定义神经网络卷积核权重 神经网络被深度学习者深深喜爱,究其原因之一是神经网络的便利性,使用者只需要根据自己的需求像搭积木一样搭建神经网络框架即可,搭建过程中我们只需要考虑卷积核的尺寸,输入输出 ...