首页 > 其他分享 >Luogu P4421 [COCI2017-2018#1] Lozinke

Luogu P4421 [COCI2017-2018#1] Lozinke

时间:2022-10-25 14:02:36浏览次数:102  
标签:rt tmp COCI2017 Luogu ++ son int P4421 include


题目链接:​​传送门​

一开始直接AC自动机每个串暴力跳fail
显然会T,44分

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
struct node {
int son[26], w, fail;
}t[A];
node tmp[A];
int n, ans, tot; char s[A][15];
void insert(char *s, int rt = 0) {
for (int i = 0; s[i]; i++) {
int x = s[i] - 'a';
if (!t[rt].son[x]) t[rt].son[x] = ++tot;
rt = t[rt].son[x];
}
t[rt].w++;
}
void getfail() {
queue<int> q;
for (int i = 0; i < 26; i++) if (t[0].son[i]) q.push(t[0].son[i]);
while (!q.empty()) {
int fr = q.front(); q.pop();
for (int i = 0; i < 26; i++) {
if (t[fr].son[i]) {
t[t[fr].son[i]].fail = t[t[fr].fail].son[i];
q.push(t[fr].son[i]);
}
else t[fr].son[i] = t[t[fr].fail].son[i];
}
}
}
int acm(char *s) {
int rt = 0, ans = 0;
memcpy(tmp, t, sizeof(t));
for (int i = 0; s[i]; i++) {
int x = s[i] - 'a';
rt = t[rt].son[x];
for (int p = rt; p and ~tmp[p].w; p = t[p].fail) ans += tmp[p].w, tmp[p].w = -1;
}
return ans;
}

int main(int argc, char const *argv[]) {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%s", s[i]) , insert(s[i]);
getfail();
for (int i = 1; i <= n; i++) ans += acm(s[i]) - 1;
cout << ans << endl;
}

然后STL开O2搞过去
把每个子串放到set里
map累计出现次数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
string s[A]; int n, ans;
map<string, int> m;
set<string> v;

int main(int argc, char const *argv[]) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i]; int len = s[i].length();
v.clear();
for (int j = 0; j < len; j++) {
string tmp = "";
for (int k = j; k < len; k++) {
tmp += s[i][k];
v.insert(tmp);
}
}
for (auto it = v.begin(); it != v.end(); it++) m[*it]++;
}
for (int i = 1; i <= n; i++) ans += m[s[i]] - 1;
cout << ans << endl;
}


标签:rt,tmp,COCI2017,Luogu,++,son,int,P4421,include
From: https://blog.51cto.com/lyle/5794672

相关文章

  • Luogu P3182 [HAOI2016]放棋子
    题目链接:​​传送门​​题目说了每行有一个障碍两个障碍不在同一行也不在同一列那障碍放哪里就没关系了矩阵都不用输入或者这样理解:交换矩阵的某两行对答案是没有影响......
  • Luogu P1772 [ZJOI2006]物流运输
    题目链接:​​传送门​​很麻烦也很难想的一道题数据很小大胆yy详细解释在代码里#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<co......
  • Luogu P4149 [IOI2011]Race
    题目链接:​​传送门​​#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<algorithm>#include<climits>#include<......
  • Luogu P2859 [USACO06FEB]摊位预订Stall Reservations
    题目链接:​​传送门​​很明显的要贪心从左右端点考虑先排序保证单调性每次往后看有没有能接上的单调性才保证了这个往后看的复杂度于是就很好写了/***@Date:2019......
  • Luogu P3488 [POI2009]LYZ-Ice Skates
    题目链接:​​传送门​​号脚的人可以穿大小的鞋设为号脚的人的数量假设选择区间就要满足把提出来即维护一个全局最大子段和就好了#include<iostream>#include<cstdi......
  • Luogu P4105 [HEOI2014]南园满地堆轻絮
    题目链接:​​传送门​​明显的二分简单的check我的没有longlong会炸掉50分#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<comple......
  • Luogu P3313 [SDOI2014]旅行
    题目链接:​​传送门​​动态开点+树剖的模板吧。都很熟的话就挺好写的特别注意在dfs序上修改#include<iostream>#include<cstdio>#include<cstring>#include<cstdli......
  • Luogu P2412 查单词
    题目链接:​​传送门​​做完这个题感觉我是个沙雕在越做越麻烦的道路上一去不复返我真傻,真的(会有大量冗余变量)#include<iostream>#include<cstdio>#include<cstring>......
  • 【luogu ARC106E】Medals(二分)(高维前缀和)
    Medals题目链接:luoguARC106E题目大意有n个第i个人的出现规律是对于所有2aik+1~2ai(k+1)的区间,2aik+1~2aik+ai会出现,另一部分则会不见。每个时间点你可以选择一......
  • 【luogu AGC035E】Develop(分类讨论)(DP)
    Develop题目链接:luoguAGC035E题目大意一开始有-1e18~1e18的所有整数,然后你每次操作可以在1~N中选一个还在的数x,擦掉他,然后查看x-2,x+K,如果没有就把数加上。然后......