HDU 5769 Substring
来源:互联网 发布:手机淘宝怎样上架宝贝 编辑:程序博客网 时间:2024/05/23 19:44
Problem Description
?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings.
But ?? thinks that is too easy, he wants to make this problem more interesting.
?? likes a character X very much, so he wants to know the number of distinct substrings which contains at least one X.
However, ?? is unable to solve it, please help him.
But ?? thinks that is too easy, he wants to make this problem more interesting.
?? likes a character X very much, so he wants to know the number of distinct substrings which contains at least one X.
However, ?? is unable to solve it, please help him.
Input
The first line of the input gives the number of test cases T;T test cases follow.
Each test case is consist of 2 lines:
First line is a character X, and second line is a string S.
X is a lowercase letter, and S contains lowercase letters(‘a’-‘z’) only.
T<=30
1<=|S|<=10^5
The sum of |S| in all the test cases is no more than 700,000.
Each test case is consist of 2 lines:
First line is a character X, and second line is a string S.
X is a lowercase letter, and S contains lowercase letters(‘a’-‘z’) only.
T<=30
1<=|S|<=10^5
The sum of |S| in all the test cases is no more than 700,000.
Output
For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the answer you get for that case.
Sample Input
2 a abc b bbb
Sample Output
Case #1: 3 Case #2: 3HintIn first case, all distinct substrings containing at least one a: a, ab, abc. In second case, all distinct substrings containing at least one b: b, bb, bbb.
用后缀自动机搞定本质不同的子串,然后二分一下统计有几个包含给出的字母。
#include<set>#include<map>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-8;const int mod = 1e9 + 7;const int N = 1e5 + 10;const int INF = 0x7FFFFFFF;int T, sum[N], cas = 0;char x[2], s[N];class SAM{ const static int maxn = 500005; //节点个数 const static int size = 26; //字符的范围 const static char base = 'a'; //字符的基准 class node { public: node *fa, *next[size]; int len, cnt; node* clear(int x) { fa = 0; len = x; cnt = 0; memset(next, 0, sizeof(next)); return this; } }nd[maxn]; //节点的设置 node *root, *last; //根节点,上一个节点 int tot; //总节点数 public: void clear() { last = root = &nd[tot = 0]; nd[0].clear(0); nd[0].cnt = 1; } //初始化 int insert(char ch) { node *p = last, *np = nd[++tot].clear(p->len + 1); last = np; int x = ch - base; while (p&&p->next[x] == 0) p->next[x] = np, p = p->fa; if (p == 0) { np->fa = root; return np->cnt = np->len - np->fa->len; } node* q = p->next[x]; if (p->len + 1 == q->len) { np->fa = q; return np->cnt = np->len - np->fa->len; } node *nq = nd[++tot].clear(p->len + 1); memcpy(nq->next, q->next, sizeof q->next); nq->fa = q->fa; q->fa = np->fa = nq; while (p &&p->next[x] == q) p->next[x] = nq, p = p->fa; return np->cnt = np->len - np->fa->len; } //插入操作 }sam;int main(){ scanf("%d", &T); while (T--) { scanf("%s%s", x, s + 1); sam.clear(); LL ans = sum[0] = 0; for (int i = 1; s[i]; i++) sum[i] = sum[i - 1] + (s[i] == x[0]); for (int i = 1; s[i]; i++) { int x = sam.insert(s[i]); int l = 1, r = x; while (l <= r) { int mid = l + r >> 1; if (sum[i] - sum[mid - 1]) l = mid + 1; else r = mid - 1; } ans += l - 1; } printf("Case #%d: %lld\n", ++cas, ans); } return 0;}
0 0
- HDU 5769 Substring
- HDU 5769 Substring
- HDU 5769 Substring(后缀数组)
- hdu-5769-Substring-后缀数组
- hdu 5769 Substring 后缀数组
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring (后缀数组)
- Hdu-5769 Substring (SA后缀数组)
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring (后缀数组)
- HDU-5769-Substring(后缀数组)
- HDU 5769 Substring 多校赛 (后缀数组)
- HDU 5769 Substring(后缀数组)
- HDU 5769 Substring (后缀数组)
- hdu 3925 substring
- hdu 3925 substring
- HDU 1238 Substring
- 记一次安装ubuntu 14.04的经历
- js div拖拽示例
- 勾股定理一日一证连载56
- SimpleDateFormat 的线程安全问题与解决方案
- SPager 三个分页架构Demo
- HDU 5769 Substring
- 时间序列分析
- 劝学(集句)
- git 本地库关联到github
- android垃圾回收garbage collection
- HDU-The All-purpose Zero(LIS)
- yarn之调度
- 用Qt Creator实现Qt对话框上的菜单
- C++中struct的使用