我是靠谱客的博主 潇洒火,这篇文章主要介绍【扩展kmp+优先队列(结构体)排序】匹配格式,现在分享给大家,希望可以做个参考。

大致思路

耶这道我自己比较顺利地做出来了。

为什么联想到要用拓展kmp呢?其实你看看,首先是有“头尾匹配”的,只不过花样多了点。

这道题我是通过getnext()先把所有next值得到。需要注意“不重复”的题意要求,所以自己由此加了些限制条件:

复制代码
1
2
nextt[i]+1<=i //中间部分与头不重复
复制代码
1
n-q.top().len+1>q.top().i+q.top().len-1 //中间部分与尾不重复

然后一个优化是“从大到小枚举”,首先这个长度肯定是要小于等于n/3的,而且我要找最大,所以从大到小开始枚举,符合条件就立马结束程序即可。所以这里用到“优先队列”,想让其以长度从大到小排序。这里优先队列的元素是结构体,自己赶紧去温习了一下之前写的博客,原来对于这种情况,是让结构体定义中多加一个bool operator<函数,然后男左女右女士优先原则。

自己觉得!!真的这种题,干瞪眼在那里想真的不如拿起笔写几个例子!!其实写例子分析其中关系就是在转化为数学思维考虑了!!对于写程序给思路和全面考虑情况很有帮助。以后没思路就多写例子吧。


AC代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream> #include<bits/stdc++.h> using namespace std; #include<queue> const int maxn=1e6+5; int nextt[maxn]; int n; char t[maxn]; void getnext() { nextt[1]=n; int a=0; for(int i=1;t[i]==t[i+1];i++) a++; nextt[2]=a; int k=2,p,l; for(int i=3;i<=n;i++) { p=k+nextt[k]-1; l=nextt[i-k+1]; if(p-i>=l) nextt[i]=l; else { int j=p-i+1; if(j<0) j=0; while(i+j<=n && t[1+j]==t[i+j]) j++; nextt[i]=j; } k=i; } } struct node { int i;int len; bool operator < (const node &a) const { return len<a.len || (len==a.len && i>a.i); } node(int ii,int ll) { i=ii; len=ll; } }; priority_queue<node> q; int main() { scanf("%s",t+1); n=strlen(t+1); getnext(); for(int i=1;i<=n;i++) { if(nextt[i]!=0 && nextt[i]<=n/3 && nextt[i]+1<=i) { // cout<<i<<" "<<nextt[i]<<endl; q.push(node(i,nextt[i])); } } while(q.empty()==false) { // cout<<"!"<<q.top().i<<" "<<q.top().len<<endl; if(nextt[n-q.top().len+1]==q.top().len) { if(n-q.top().len+1>q.top().i+q.top().len-1) { cout<<q.top().len; return 0; } } q.pop(); } return 0; }
刚开始因为没在主函数里调用getnext() 调试了两三次.....别再这样了拜托T T.....


最后

以上就是潇洒火最近收集整理的关于【扩展kmp+优先队列(结构体)排序】匹配格式的全部内容,更多相关【扩展kmp+优先队列(结构体)排序】匹配格式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部