cf 579 div3 d1 d2 e

cf 579 div3 d1 d2 e

D1 D2

The meaning of problems

You give a string s and t are strings, a request to delete the longest substring, so that the sub-sequence s t is still seeking to remove a length of the longest substring

answer

With L and R t each array element to record the position of the earliest and latest appeared. MAXN initialized to max (R [0], | s | - L [| t | -1] - 1) is deleted substring represented at both ends of the sequence, with maxn = max (R [i] - L [i -1] - 1, maxn) represents an intermediate deletion sequence substringAh, probably so to speak

#include <cstdio>
#include <cstring>

int main() {
    char s[200010], t[200010];
    int l[200010], r[200010];
    scanf("%s %s", s, t);
    int n = strlen(s), m = strlen(t);
    for(int i = 0, j = 0; i < n && j < m; i++) {
        if(s[i] == t[j]) {
            l[j] = i;
            j++;
        }
    }
    for(int i = n - 1, j = m - 1; j >= 0 && i >= 0; i--) {
        if(s[i] == t[j]) {
            r[j] = i;
            j--;
        }
    }
    int maxn;
    if(n - l[m-1] - 1 > r[0]) maxn = n - l[m-1] - 1;
    else maxn = r[0];
    for(int i = 1; i < m; i++) {
        if(r[i] - l[i-1] - 1> maxn) maxn = r[i] - l[i-1] - 1;
    }
    printf("%d\n", maxn);
    return 0;
}

E

The meaning of problems

You give a sequence, the sequence number can be changed to -1 or +1 his number, but must be greater than 0, such as 1 only become 2, 1 and 2 or 3 can be turned, the different sequences required The maximum number of how many?

answer

If a [i] is the first time (cnt [a [i]] == 0), then if a [i] not had a -1, then put a [i] becomes a [i] - 1, a [i] -1 has appeared, then it unchanged.

If a [i] is not the first time, then (cnt [a [i]]> 0), put a [i] becomes a [i] + 1'd

CNT [a [i]] represents a [i] is the number of

#include <cstdio>
#include <algorithm>
using std::sort;
 
int cnt[150010], a[150010];
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    sort(a, a + n);
    for(int i = 0; i < n; i++) {
        if(cnt[a[i]]) cnt[a[i]+1]++;
        else if(cnt[a[i] - 1] == 0 && a[i] > 1) cnt[a[i] - 1]++;
        else cnt[a[i]]++;
    }
    int ans = 0;
    for(int i = 1; i < 150002; i++) {
        if(cnt[i]) ans++;
    }
    printf("%d\n", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fanshhh/p/11355015.html