NOIP2022模拟赛by Cqy1

T1

询问一个字符串任意子串出现最多次数。

Solution

诈骗,输出出现字母出现最多的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn
#define put() putchar('\n')
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
string s;
int t[305];
signed main(void){
//freopen("1.in","r",stdin);
int i,Max=0;
cin>>s;int n=s.size();
for (i=0;i<n;i++) t[s[i]]++,Max=max(Max,t[s[i]]);
printf("%d",Max);
return 0;
}

T2

字典树。

找到链最长并且字典序最大的串在树上打上标记最后输出。

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
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 50005
#define put() putchar('\n')
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
int cnt=1,n,root=1;
struct yyy{
int nex[26],flag,v;
}a[maxn*20];
int maxlen;string Maxs;
inline void insert(string s) {
int i,now=root,len=s.size();
if (maxlen<len||(maxlen==len&&s>Maxs)) maxlen=len,Maxs=s;
for (i=0;i<len;i++) {
if (!a[now].nex[s[i]-'a']) a[now].nex[s[i]-'a']=++cnt;
now=a[now].nex[s[i]-'a'];
}
a[now].flag++;
}

inline void update(string s) {
int i,now=root,len=s.size();
for (i=0;i<len;i++) {
now=a[now].nex[s[i]-'a'];
a[now].v++;
}
}
char ans[2000005];int tot;
inline void solve(int now) {
if (a[now].flag) ans[++tot]='P';
int tmp=-1;
for (int i=0;i<=25;i++) {
if (a[now].nex[i]) {
if (a[a[now].nex[i]].v==1) assert(tmp==-1),tmp=i;
else ans[++tot]=('a'+i),solve(a[now].nex[i]),ans[++tot]='-';
}
}
if (tmp!=-1) ans[++tot]=('a'+tmp),solve(a[now].nex[tmp]);
}
signed main(void){ int i;
string s;
read(n);
for (i=1;i<=n;i++) {
cin>>s,insert(s);
}
update(Maxs);
solve(root);
printf("%d\n",tot);for (i=1;i<=tot;i++) putchar(ans[i]),put();
return 0;
}

T3

CF1286E 线段树,KMP,待补

T4

SAM,补nm

2022.07.24