Palinwords (处理回文串)

题目链接:https://vjudge.net/contest/344930#problem/G

 

题目大意:给你字符串,如果他包含至少两个长度大于等于3的回文,并且这些回文不能嵌套(例如aaa嵌套在aaaa,waw嵌套在awawa),如果这个字符串这么牛逼的话,就输出他。

 

题目思路:其实这道题有一个贪心的思想在里面,其实每次我们只需要去找长度为3和长度为4的回文串就好了。这里的处理策略就是先处理长度为3的回文串,再处理长度为4的回文串。处理长度为4的回文串的时候,要避免它的子串也是一个回文串。

 

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <stdlib.h>
 5 #include <string>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 
14 
15 #define INF 0x3f3f3f3f
16 #define LL long long
17 
18 typedef unsigned long long ull;
19 const int maxn = 1e5+10;
20 
21 char s[maxn];
22 ull base = 131;
23 ull mod = 1e9+7;
24 ull p[maxn];
25 ull h1[maxn],h2[maxn];
26 ull q[maxn];
27 
28 
29 ull get_hash(ull h[],int l,int r){
30     return (h[r] - h[l-1]*p[r-l+1]);
31 }
32 
33 bool check(char s[]) {
34     std::set<ull> st;
35     int ans = 0;
36     int len = strlen(s+1);
37     for (int i=2;i+1<=len;i++) {
38         if (s[i-1] == s[i+1]) {
39             ull temp = get_hash(h1,i-1,i+1);
40             if (st.find(temp) == st.end()) {
41                 st.insert(temp);
42                 ans++;
43             }
44         }
45     }
46 
47     for (int i=1;i+3<=len;i++) {
48         if (s[i] == s[i+3] && s[i+1] == s[i+2]) {
49             ull a = get_hash(h1,i,i+3);
50             ull b = get_hash(h1,i+1,i+3);
51             ull c = get_hash(h1,i,i+2);
52             if (st.find(a) == st.end() && st.find(b) == st.end() && st.find(c) == st.end()) {
53                 ans++;
54                 st.insert(a);
55                 st.insert(b);
56                 st.insert(c);
57             }
58         }
59     }
60     return ans >= 2;
61 }
62 
63 int main() {
64     p[0] = 1;
65     for (int i=1;i<maxn;i++) {
66         p[i] = p[i-1] * base;
67     }
68     while (~scanf("%s",s+1)) {
69         int len = strlen(s+1);
70         for (int i=1;i<=len;i++) {
71             h1[i] = h1[i-1] * base + s[i];
72         }
73         if (check(s)) {
74             printf("%s\n",s+1);
75         }
76     }
77     return 0;
78 }

 

全部评论

相关推荐

压力很大,面试官全程高压,问的问题不难,但是没有任何反馈,很慌张,也无算法。实习问了20分钟,一直问我你们做的有什么用,总时长一小时1.学校都有什么课程2.spring的ioc原理以及优点3.除了解耦还知道什么?4.springboot与spring区别,二者的源码看过没?Tomcat了解嘛?有没有具体看过5.spring的bean,面试官一直在重复一个思想问我懂不懂,完全没听过6.mybatis是干什么的?ibatis用过没?平常怎么写SQL?完全不写嘛?7.设计一个分布式双十一秒杀系统(前端,网关,缓存,数据库防超卖全设计)8.怎么做限流9.缓存与数据库一致性,你做异步要用户等你嘛?10.负载均衡怎么做11.多数据中心还是单数据中心,如果出现没卖完怎么做(到这完全不会了,面试官直接说换个话题吧)12.平常读书吗?13.上过哲学课嘛?14.兴趣爱好有没有15.对ai的看法16.来深圳有问题嘛?17.为什么不考研18.上大学带给了你什么?你提升在哪里,有没有具体的例子?反问:1.现在手机都有应用市场,应用宝怎么盈利?除了手机应用市场还是有人用,现在在做跨端,微软都有合作,之后会进军mac,主要做游戏,腾讯本身就是游戏大户。2.面试表现?整体评价一下会给到反馈。面完直接变HR面,今天HR面后,已经转为录用评估了,来牛客许个愿,暑期现在还没什么面试,希望能拿个offer之后再考虑要不要留在手子吧。
nunuking:三面压力这么大吗,面试的会议约了多长时间呀
面试问题记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务