[Offer收割]编程练习赛54 register

Ended

Participants:154

Verdict:Accepted
Score:100 / 100
Submitted:2018-04-08 13:51:58

Lang:G++

Edit
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
#include <bits/stdc++.h>
using namespace std;
char s[500000+5];
int main() {
    scanf("%s", &s);
    int len = strlen(s);
    int ans = len+1;
    if (len & 1) {
        int pos = len-1, cnt = 0;
        for (int i = len/2-1, j = len/2; i >= 0 && cnt < 2; --i, ++j)
            if (s[i] != s[j]) pos = j, ++cnt, ++j;
        if (cnt <= 1) ans = min(ans, pos+1);
        pos = 0, cnt = 0;
        for (int i = len/2, j = len/2+1; i > 0 && cnt < 2; --i, ++j)
            if (s[i] != s[j]) pos = i, ++cnt, --i;
        if (cnt <= 1) ans = min(ans, pos+1);
    } else {
        int pos = len-1, cnt = 0;
        for (int i = len/2-2, j = len/2; i >= 0 && cnt < 2; --i, ++j)
            if (s[i] != s[j]) pos = j, ++cnt, ++j;
        if (cnt <= 1) ans = min(ans, pos+1);
        pos = 0, cnt = 0;
        for (int i = len/2-1, j = len/2+1; i > 0 && cnt < 2; --i, ++j)
            if (s[i] != s[j]) pos = i, ++cnt, --i;
        if (cnt <= 1) ans = min(ans, pos+1);
    }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX