0%

CodeTON Round 9 (Div. 1 + Div. 2, Rated, Prizes!)——B. Shohag Loves Strings(选取该字符串中的子串,使该子串的子串数为偶数)

思路讲解

赛时其实什么都想到了,但因为代码编写的问题没过,唉,不多讲了。

image

唉,主要还是我不太熟悉0-based。

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
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=static_cast<int>(2e5)+15;
int T,n;

signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while (T--) {
string s;
cin>>s;
if(s.size()==1) {
cout<<-1<<endl;
continue;
}
int n=s.size();bool isBreak=false;
for(int i=0;i<n-1;++i) {
if(s[i]==s[i+1]) { // ww pp 这种是可以的
cout<<s[i]<<s[i+1]<<endl;
isBreak=true;
break;
}
}
if(isBreak)
continue;
if(s.size()==2) {
cout<<-1<<endl;
continue;
}
// abc 可以分为a b c ab bc abc 6种 为偶数
for(int i=0;i<n-2;++i) {
if(s[i]!=s[i+1] && s[i]!=s[i+2] && s[i+1]!=s[i+2]) {
cout<<s[i]<<s[i+1]<<s[i+2]<<endl;
isBreak=true;
break;
}
}
if(isBreak)
continue;
// 能够逃过上面两重判定 必然是类似于abababab的 这种是无解的
cout<<-1<<endl;
}
return 0;
}
// AC https://codeforces.com/contest/2039/submission/293064839

心路历程(WA,TLE,MLE……)