Codeforces Round #306 (Div. 2) A B C

来源:互联网 发布:围巾淘宝加盟 编辑:程序博客网 时间:2024/05/22 05:12

A - Two Substrings 题意:字符串中是否有BA 和AB且字母不重叠

暴力for过去找即可

#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <string>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1#define ls rt << 1#define rs rt << 1 | 1#define pi acos(-1.0)#define eps 1e-8#define asd puts("sdfsdfsdfsdfsdfsdf");typedef long long ll;typedef __int64 LL;const int inf = 0x3f3f3f3f;const int N = 100010;char a[N];int main(){    while( ~scanf("%s", a) ) {        int len = strlen( a );        if( len < 4 ) {            puts("NO");            continue;        }        bool OK = 0;        for( int i = 0; i < len-1; ++i ) {            if( a[i] == 'A' && a[i+1] == 'B' ) {                for( int j = i+2; j < len-1; ++j ) {                    if( a[j] == 'B' && a[j+1] == 'A' ) {                        OK = 1;                        break;                    }                }                break;            }        }        for( int i = 0; i < len-1; ++i ) {            if( a[i] == 'B' && a[i+1] == 'A' ) {                for( int j = i+2; j < len-1; ++j ) {                    if( a[j] == 'A' && a[j+1] == 'B' ) {                        OK = 1;                        break;                    }                }                break;            }        }        if( OK )            puts("YES");        else            puts("NO");    }    return 0;}

B - Preparing Olympiad 题意:n道题任取几道,使难度总和在 【l, r】,最大难度的题-最小的 >= x,的情况数量

枚举所以得题目情况,依次判断即可

const int N = 20;int a[N];int n, l, r, x;int main(){    while( ~scanf("%d%d%d%d", &n, &l, &r, &x) ) {        for( int i = 0; i < n; ++i )            scanf("%d", &a[i]);        int ans = 0;        for( int i = 0; i < (1<<n); ++i ) {            int L = inf, R = -1;            int cnt = 0, tmp = 0;            for( int j = 0; j < n; ++j ) {                if( i & (1<<j) ) {                    L = min( L, a[j] );                    R = max( R, a[j] );                    tmp += a[j];                    cnt++;                }            }            if( R-L >= x && cnt >= 2 && tmp <= r && tmp >= l )                ans++;        }        printf("%d\n", ans);    }    return 0;}

C - Divisibility by Eight题意:100位的数,可以移除一些位数,但是相对位子不能变,问有没有存在情况使移除(或者不移除)后的被8整除。

首先考虑到,被8整除的数,后三位一定能被8整除,那么暴力枚举后三位就行了,O(n^3)。

const int N = 105;char a[N];int main(){    while( ~scanf("%s", a)) {        int len = strlen(a);        bool OK = 0;        int x;        for( int i = 0; i < len; ++i ) {            if( a[i] == '0' || a[i] == '8' ) {                x = a[i] - '0';                OK = 1;                break;            }            for( int j = i+1; j < len; ++j ) {                x = 0;                x = x * 10 + a[i] - '0';                x = x * 10 + a[j] - '0';                if( x % 8 == 0 ) {                    OK = 1;                    break;                }                for( int k = j+1; k < len; ++k ) {                    x = 0;                    x = a[i] - '0';                    x = x * 10 + a[j] - '0';                    x = x * 10 + a[k] - '0';                    if( x % 8 == 0 ) {                        OK = 1;                        break;                    }                }                if( OK )                    break;            }            if( OK )                break;        }        if( OK )            printf("YES\n%d\n", x);        else            puts("NO");    }    return 0;}


0 0