Codeforces Beta Round #8

简介: 点击打开链接 A 思路:正反字符串各自判断一次是否有对应的两个子串 分析: 1 题目给定一个字符串str,然后给定两个不同时间段内看到的子串s1 , s2,判断是哪一种情况。

点击打开链接


A

思路:正反字符串各自判断一次是否有对应的两个子串

分析:
1 题目给定一个字符串str,然后给定两个不同时间段内看到的子串s1 , s2,判断是哪一种情况。
2 我们知道两个时间段内那么看到的字符串是有间隔的,那么如果我们怎么知道是否是“向前”“向后”“都有”“没有”这四种答案中的哪一种呢?其实我们知道如果给定的两个子串都是给定的字符串str的子串,那么我们就必须正反都判断是否都能按照顺序看到s1 和 s2.
3 利用一个变量记录正反两面的情况,然后逐一判断

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 100010;
const int N = 110;
char str[MAXN];
char s1[N] , s2[N];

bool solve(){
     int len = strlen(str);
     int len_1 = strlen(s1) , len_2 = strlen(s2);
     int pos;
     bool mark = false;
     char tmp[MAXN];
     for(int i = 0 ; i < len ; i++){
        if(str[i] != s1[0]) continue;
        pos = 0;
        for(int j = i ; j < len && pos < len_1 ; j++ , pos++){
           if(str[j] != s1[pos]) break;
        }
        if(pos == len_1){
          pos = i+len_1;
          mark = true;
          break;
        }
     }
     if(!mark) return false;
     for(int i = pos ; i < len ; i++){
        if(str[i] != s2[0]) continue;
        pos = 0;
        for(int j = i ; j < len && pos < len_2 ; j++ , pos++){
           if(str[j] != s2[pos]) break;
        }
        if(pos == len_2) return true;
     } 
     return false;
}

int main(){
    bool isLeft , isRight;
    while(scanf("%s" , str) != EOF){
        scanf("%s%s" , s1 , s2);
        isLeft = solve();
        reverse(str , str+strlen(str));
        isRight = solve();
        if(isLeft && !isRight)  printf("forward\n");
        if(!isLeft && isRight)  printf("backward\n");
        if(isLeft && isRight)   printf("both\n");
        if(!isLeft && !isRight) printf("fantasy\n");
    }
    return 0;
}


B

思路:枚举路线进行判断
分析:
1 题目的意思是说是否存在一个地图使得这个线路是起点到终点的最短路线
2 很明显我们需要每一步进行判断,那么怎么判断当前步不满足呢?请看下图

那么我们知道假设当前点的周围总共有4个点,它的前面一个点肯点是走过了,如果还存在
另外一个点也是走过的那么肯定是不满足的,因为这个走过的点可以直接到当前点而不是饶了一圈;还有一种情况就是回到之前走过的点,也就是当前点之前已经走过也是不满足的。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 310;
const int N = 110;
int vis[MAXN][MAXN];
char str[N];

bool judge(){
    int len = strlen(str);
    if(len == 1) return true;
    int row = 105 , col = 105;
    memset(vis , 0 , sizeof(vis));
    vis[row][col] = 1;
    for(int i = 0 ; i < len ; i++){
       if(str[i] == 'L') col--;
       if(str[i] == 'R') col++;
       if(str[i] == 'U') row--;
       if(str[i] == 'D') row++;
       int cnt = vis[row][col]+vis[row-1][col]+vis[row][col+1]+vis[row+1][col]+vis[row][col-1];
       if(cnt > 1) return false;
       vis[row][col] = 1;
    }
    return true;
}

int main(){
    bool ans;
    while(scanf("%s" , str) != EOF){
        ans = judge();
        printf("%s\n" , ans ? "OK" : "BUG");
    }
    return 0;
}




目录
相关文章
Codeforces Round #628 解补题报告
Codeforces Round #628 解补题报告
58 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output ...
1227 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output...
1273 0
|
人工智能
Codeforces Beta Round #2 A,B,C
A. Winner time limit per test:1 second memory limit per test:64 megabytes input:standard input output:standard output ...
1204 0
|
Perl
Codeforces Beta Round #1 A,B,C
A. Theatre Square time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard ...
850 0
|
人工智能 vr&ar
Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
                              B. Rebranding The name of one small but proud corporation consists of n lowercase English letters.
925 0
容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
  Lengthening Sticks Problem's Link:  http://codeforces.com/contest/571/problem/A   Mean:  给出a,b,c,l,要求a+x,b+y,c+z构成三角形,x...
903 0
Codeforces Beta Round #1
点击打开链接 A #include #include using namespace std; long long n , m , a; int main(){ long long sum; while(cin>>...
902 0

热门文章

最新文章