https://vjudge.net/contest/175596#overview

A.设第i次出现的位置左右端点分别为Li,Ri

初始化L0 = 0,则有ans = sum{ (L[i] - L[i-1]) * (n + 1 - Ri) }

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int last = ; char s[]; long long ans; int main() {
scanf("%s", s + );
int n = strlen(s + );
for(int i = ;i <= n;i ++) {
if(i + <= n && s[i] == 'b' && s[i + ] == 'e' && s[i + ] == 'a' && s[i + ] == 'r') {
ans += 1ll * (i - last) * (n + - i - );
last = i;
i += ;
}
}
cout << ans;
return ;
}

B.AC自动机板子题,我的板子常数很大

 #include <queue>
#include <cstdio>
#include <cstring> using namespace std; const int maxn = ; struct trie {
int next[maxn][], fail[maxn], end[maxn];
int L, root;
queue <int> q; int newnode() {
for(int i = ;i < ;i ++)
next[L][i] = -;
end[L] = ;
return L ++;
} void clear() {
L = ;
root = newnode();
} int idx(char c) {
return c - 'a';
} void insert(char *buf) {
int len = strlen(buf), now = root, c;
for(int i = ;i < len;i ++) {
c = idx(buf[i]);
if(next[now][c] == -)
next[now][c] = newnode();
now = next[now][c];
}
end[now] ++;
} void build() {
for(int i = ;i < ;i ++) {
if(next[root][i] == -)
next[root][i] = root;
else {
fail[next[root][i]] = root;
q.push(next[root][i]);
}
}
while(!q.empty()) {
int now = q.front();
q.pop();
for(int i = ;i < ;i ++) {
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else {
fail[next[now][i]] = next[fail[now]][i];
q.push(next[now][i]);
}
}
}
} int query(char *buf) {
int len = strlen(buf), now = root, res = , tmp;
for(int i = ;i < len;i ++) {
tmp = now = next[now][idx(buf[i])];
while(tmp != root) {
res += end[tmp];
end[tmp] = ;
tmp = fail[tmp];
}
}
return res;
}
}; trie ac; int Case, n; char buf[]; int main() {
scanf("%d", &Case);
while(Case --) {
scanf("%d", &n), ac.clear();
while(n --) scanf("%s", buf), ac.insert(buf);
scanf("%s", buf), ac.build();
printf("%d\n", ac.query(buf));
}
return ;
}

C.考察对KMP中next数组的理解,由一个串重复而来

所以就是max(i - next[i])

 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; char s[]; int nex[]; int main() {
int i, j, ans, len;
while(~scanf("%s", s)) {
len = strlen(s);
nex[] = -;
ans = ;
for(i = ;i < len;i ++) {
j = nex[i - ];
while(j >= && s[j + ] != s[i]) j = nex[j];
if(s[j + ] == s[i]) {
nex[i] = j + ;
ans = max(ans, i - nex[i]);
}
else nex[i] = -, ans = max(ans, i + );
}
printf("%d\n", ans);
}
return ;
}

D.令a[i] -= a[i + 1],题目就变成了

求数列中出现次数不小于2次的最长重复子串

后缀数组一个典型问题,二分子串长度即可

(考场上观察了半天手里板子的接口...然后放弃了)

E.先假设要由空串刷成串2,区间DP即可

dp[i][j]代表把 i-j 这段刷成串2需要的最少次数

可能分成几段分开去刷,所以不能直接ans = dp[L][R] (s1[L] != s2[L],s1[R] != s2[R])

利用f[i]代表 1-i 这段由串1刷成串2的最少次数

ans = f[n]

 #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n, dp[][], f[]; char s1[], s2[]; int main() {
while(~scanf("%s %s", s1 + , s2 + )) {
n = strlen(s1 + );
memset(dp, 0x3f, sizeof dp);
for(int d = ;d <= n;d ++)
for(int i = ;i + d - <= n;i ++) {
int j = i + d - ;
if(i == j) dp[i][i] = ;
else if(j == i + ) dp[i][j] = - (s2[i] == s2[j]);
else {
for(int k = i;k < j;k ++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + ][j] - (s2[i] == s2[k + ]));
}
}
for(int i = ;i <= n;i ++) {
f[i] = dp[][i];
if(s1[i] == s2[i]) f[i] = min(f[i - ], f[i]);
else
for(int j = ;j < i;j ++)
f[i] = min(f[i], f[j] + dp[j + ][i]);
}
printf("%d\n", f[n]);
}
return ;
}

F.简单的字典树

 #include <cstdio>
#include <cstring> const int maxn = ; struct trie {
int ch[maxn][];
int val[maxn];
int siz; void init() {
siz = ;
memset(ch, , sizeof ch);
memset(val, , sizeof val);
} void insert(int x) {
int i, u, c;
int num[] = {};
for(i = ;i < ;i ++)
num[i] = x & ( << i);
for(i = u = ;i < ;i ++) {
c = (num[ - i] != );
if(!ch[u][c]) ch[u][c] = siz ++;
u = ch[u][c], val[u] ++;
}
val[] ++;
} void de1ete(int x) {
int i, u, c;
int num[] = {};
for(i = ;i < ;i ++)
num[i] = x & ( << i);
for(i = u = ;i < ;i ++) {
u = ch[u][(num[ - i] != )];
val[u] --;
}
val[] --;
} void query(int x) {
int i, u, c, ans = ;
int num[] = {};
for(i = ;i < ;i ++)
num[i] = x & ( << i);
for(i = u = ;i < ;i ++) {
c = !(num[ - i] != );
if(ch[u][c] && val[ch[u][c]]) ans |= ( << ( - i)), u = ch[u][c];
else u = ch[u][!c];
}
printf("%d\n", ans);
}
}; trie now; int n, x; char str[]; int main() {
now.init();
now.insert();
scanf("%d", &n);
while(n --) {
scanf("%s %d", str, &x);
switch(str[]) {
case '+':now.insert(x);break;
case '-':now.de1ete(x);break;
case '?':now.query(x);break;
}
}
return ;
}

G.

H.

I.

J.

K.最长回文子串,直接上马拉车

板子不长,mp[i] - 1 表示以 i 为中心的最长回文串长度

 #include <map>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = ; char str[], s[maxn], ma[maxn], ans[maxn]; int mp[maxn], l, len; map <char, char> p; void manacher() {
l = ;
ma[l ++] = '$';
ma[l ++] = '#';
for(int i = ;i < len;i ++)
ma[l ++] = s[i], ma[l ++] = '#';
ma[l] = ;
int mx = , id = ;
for(int i = ;i < l;i ++) {
mp[i] = mx > i ? min(mp[ * id - i], mx - i) : ;
while(ma[i + mp[i]] == ma[i - mp[i]]) mp[i] ++;
if(i + mp[i] > mx) mx = i + mp[i], id = i;
}
} int main() {
while(~scanf("%s %s", str, s)) {
len = strlen(s);
manacher();
int leng = , pos = -;
for(int i = ;i < l;i ++)
if(mp[i] > leng)
leng = mp[i], pos = i;
leng --;
if(leng == ) {
puts("No solution!");
continue;
}
if(pos & ) {
printf("%d %d\n", pos / - leng / , pos / - leng / + leng - );
for(int i = pos / - leng / , j = ;j <= leng;i ++, j ++)
ans[j] = s[i];
}
else {
printf("%d %d\n", pos / - leng / - , pos / - leng / + leng - );
for(int i = pos / - leng / - , j = ;j <= leng;i ++, j ++)
ans[j] = s[i];
}
ans[leng + ] = ;
int dis = 'a' - str[];
for(int i = ;i < ;i ++)
p['a' + i] = 'a' + (i + dis + ) % ;
for(int i = ;i <= leng;i ++)
ans[i] = p[ans[i]];
puts(ans + );
}
return ;
}

L.变换同D题,然后就是裸的KMP了

 #include <map>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = ; char str[], s[maxn], ma[maxn], ans[maxn]; int mp[maxn], l, len; map <char, char> p; void manacher() {
l = ;
ma[l ++] = '$';
ma[l ++] = '#';
for(int i = ;i < len;i ++)
ma[l ++] = s[i], ma[l ++] = '#';
ma[l] = ;
int mx = , id = ;
for(int i = ;i < l;i ++) {
mp[i] = mx > i ? min(mp[ * id - i], mx - i) : ;
while(ma[i + mp[i]] == ma[i - mp[i]]) mp[i] ++;
if(i + mp[i] > mx) mx = i + mp[i], id = i;
}
} int main() {
while(~scanf("%s %s", str, s)) {
len = strlen(s);
manacher();
int leng = , pos = -;
for(int i = ;i < l;i ++)
if(mp[i] > leng)
leng = mp[i], pos = i;
leng --;
if(leng == ) {
puts("No solution!");
continue;
}
if(pos & ) {
printf("%d %d\n", pos / - leng / , pos / - leng / + leng - );
for(int i = pos / - leng / , j = ;j <= leng;i ++, j ++)
ans[j] = s[i];
}
else {
printf("%d %d\n", pos / - leng / - , pos / - leng / + leng - );
for(int i = pos / - leng / - , j = ;j <= leng;i ++, j ++)
ans[j] = s[i];
}
ans[leng + ] = ;
int dis = 'a' - str[];
for(int i = ;i < ;i ++)
p['a' + i] = 'a' + (i + dis + ) % ;
for(int i = ;i <= leng;i ++)
ans[i] = p[ans[i]];
puts(ans + );
}
return ;
}

bupt summer training for 16 #8 ——字符串处理的更多相关文章

  1. bupt summer training for 16 #7 ——搜索与DP

    https://vjudge.net/contest/174962#overview A.我们发现重点在于x,y只要累加就ok了 在每个x上只有上下两种状态,所以可以记忆化搜索 f[0/1][i]表示 ...

  2. bupt summer training for 16 #6 ——图论

    https://vjudge.net/contest/174020 A.100条双向边,每个点最少连2个边 所以最多100个点,点的标号需要离散化 然后要求恰好经过n条路径 快速幂,乘法过程就是flo ...

  3. bupt summer training for 16 #5 ——数据结构

    https://vjudge.net/contest/173780 A.假设 Pt = i,则由Ppi = i得 Ppt = t = Pi 所以就有 if Pt = i then Pi = t #in ...

  4. bupt summer training for 16 #4 ——数论

    https://vjudge.net/contest/173277#overview A.平方差公式后变为 n = (x + y)(x - y) 令 t = x - y ,变成 n = (t + 2x ...

  5. bupt summer training for 16 #3 ——构造

    https://vjudge.net/contest/172464 后来补题发现这场做的可真他妈傻逼 A.签到傻逼题,自己分情况 #include <cstdio> #include &l ...

  6. bupt summer training for 16 #2 ——计算几何

    https://vjudge.net/contest/171368#overview A.一个签到题,用叉积来判断一个点在一条线的哪个方向 可以二分,数据范围允许暴力 #include <cst ...

  7. bupt summer training for 16 #1 ——简单题目

    D.What a Mess 给n个数,求其中能满足 a[i] % a[j] == 0 的数对之和 n = 1W,max_ai = 100W 不是很大,所以就直接筛就可以了 计算可得最高复杂度 < ...

  8. leecode刷题(16)-- 字符串转换整数

    leecode刷题(16)-- 字符串转换整数 字符串转换整数 描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格 ...

  9. GUID转换成16位字符串或19位唯一字符串

    整理几个经常使用GUID转换成16位字符串或19位唯一字符串方法: /// <summary> /// 依据GUID获取16位的唯一字符串 /// Author : 付义方 /// < ...

随机推荐

  1. ASP.NET Overview

    https://msdn.microsoft.com/en-us/library/4w3ex9c2.aspx ASP.NET is a unified统一的 Web development model ...

  2. 【POJ 3349】 Snowflake Snow Snowflakes

    [题目链接] http://poj.org/problem?id=3349 [算法] 哈希 若两片雪花相同,则它们六个角上的和一定相同,不妨令 H(A) = sigma(Ai) % P ,每次只要到哈 ...

  3. LIBTOOL is undefined 问题的解决方法

    configure.ac:10: error: possibly undefined macro: AC_PROG_LIBTOOL If this token and others are legit ...

  4. MySQL 字符编码问题详细解释

    http://www.codesoil.net/tag/charset Character Set Problem in PHP + MySQL4.1+ 和许多人一样,我也是在转移blog时才发现这个 ...

  5. Django-CKeditor使用笔记

    1. 安装django-ckeditor $ pip install django-ckeditor 2. 在setting中,添加ckeditor , ckeditor_uploader 到INST ...

  6. cookie应用(一周内免登陆)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  7. P3399 丝绸之路(线性二维dp)

    P3399 丝绸之路 题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚. ...

  8. P4049 [JSOI2007]合金

    传送门 我数学可能白学了-- 因为三个数加起来等于\(1\),那么只要用前两个数就能表示,那么就能把每一种金属看成一个二维向量.考虑只有两个向量的时候,设这两个向量为\(a,b\),那么一个向量\(c ...

  9. 【洛谷3224/BZOJ2733】[HNOI2012]永无乡 (Splay启发式合并)

    题目: 洛谷3224 分析: 这题一看\(n\leq100000\)的范围就知道可以暴力地用\(O(nlogn)\)数据结构乱搞啊-- 每个联通块建一棵Splay树,查询就是Splay查询第k大的模板 ...

  10. Ruby开发环境的搭建

    1.Ruby的下载 https://rubyinstaller.org/downloads/ 2.Ruby的安装 3.Eclipse配置Ruby开发环境 插件地址:http://rubyeclipse ...