[AtCoder Beginner Contest 103]

[AtCoder Beginner Contest 103]

A - Task Scheduling Problem

#include <bits/stdc++.h>
using namespace std;

int arr[5];
int main(){
    scanf("%d%d%d",&arr[1],&arr[2],&arr[3]);   
    sort(arr+1,arr+1+3);
    printf("%d",arr[3]-arr[1]);
    return 0;
}

B - String Rotation

复制一次处理环,kmp板子

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
const int M = 1e4+5;
int xlen,ylen;
int pre[M];
char x[M]; 
char y[N]; 

// x 是模式串,y 是文本串,均从1开始
void read_string(){
    scanf("%s",x+1);
    scanf("%s",y+1);
    xlen = strlen(x+1);
    ylen = strlen(y+1);
    for(int i = 1,j = ylen+1; i <= ylen; ++i,++j){
        y[j] = y[i];
    }
    ylen <<= 1;
}


// pre[i]: x串的长度为i的子串的公共前后缀长度
void get_pre(){
    pre[0] = -1;
    pre[1] = 0;
    for(int i = 2; i <= xlen; ++i){
        if(x[i] == x[pre[i-1]+1]){
            pre[i] = pre[i-1] + 1;   
        }else{
            int ptr = pre[i-1] + 1;
            while(ptr >= 1 && x[ptr] != x[i]){
                ptr = pre[ptr-1] + 1;
            }

            pre[i] = ptr;
        }
    }
}

bool kmp_match(){
    int xptr = 1,yptr = 1;
    while(xptr <= xlen && yptr <= ylen){
        if(xptr == 0 || x[xptr] == y[yptr]){
            ++xptr;
            ++yptr;
        }else{
            xptr = pre[xptr-1] + 1;
        }

        if(xptr == xlen + 1){  
            return true;
        }
    }

    return false;
}

int main(){
    read_string();
    get_pre();
    if(kmp_match()){
        puts("Yes");
    }else{
        puts("No");
    }
    // system("pause");
    return 0;
}

C - Modulo Summation

#include <bits/stdc++.h>
using namespace std;
int n;
int t;
int main(){
    cin >> n;
    int ans = 0;
    for(int i = 1; i <= n; ++i){
        cin >> t;
        ans += t;
    }
    cout << ans - n;

    // system("pause");
    return 0;
}

D - Islands War

最小覆盖问题

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int n,m;
typedef pair<int,int> pii;
vector<pii> vec;
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= m; ++i){
        pii p;
        scanf("%d%d",&p.first,&p.second);
        vec.push_back(p);
    }
    sort(vec.begin(),vec.end());

    int l = vec[0].first,r = vec[0].second;
    int ans = 0;

    for(int i = 0; i < m; ++i){
        while(i < m && !(vec[i].first >= r || vec[i].second <= l) ){
            l = max(l,vec[i].first);
            r = min(r,vec[i].second);
            ++i;
        }
        ++ans;
        l = vec[i].first;
        r = vec[i].second;
        --i;    
    }
    printf("%d",ans);
    // system("pause");
    return 0;
}
posted @ 2021-01-17 22:40  popozyl  阅读(114)  评论(0编辑  收藏  举报