某场CF B Octagons(模拟)

题目:http://codeforces.com/gym/100004/attachments (需下载到本地,doc格式)

题意:如图
图
这是由多个八边形(不一定为正八边形)拼接在一起的图形。八边形的边由a,b,c三个字母构成,每条边对应一个字母,且同一个八边形的边只能由2种字母构成,相邻的不相等。判断给定的一个只由abc构成的字符串,是否能在图中围成一个封闭的图形。

思路:模拟。对于2个相邻一样的或者8个在同一个八边形内的可以直接去掉。而7个,6个,5个在同一个八边形内的,可以分别替换成1个,2个,3个在同一个八边形内的短路。用string对字符串的拼接操作比较简单。
(欠下的模拟债)

代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>

using namespace std;

const int N = 1e2;

string str;

string spilt(int x, int y) {
    string tmp = str.substr(x, y - x + 1);
    if (tmp.size() == 8 || tmp.size() == 2) {
        tmp = "";
    }
    else if (tmp.size() <= 7 && tmp.size() >= 5) {
        tmp = tmp.substr(1, 8 - tmp.size());
    }
    return str.substr(0, x) + tmp + str.substr(y + 1, str.size() - y);
}

bool check2() {
    for (int i = 1; i < str.size(); i++) {
        if (str[i] == str[i - 1]) {
            str = spilt(i - 1, i);
            return true;
        }
    }
    return false;
}

bool check(int p) {
    int cnt = 0;
    for (int i = 2; i < str.size(); i++) {
        if (i - cnt == p) {
            str = spilt(cnt, i - 1);
            cnt = i;
            return true;
        }
        if (str[i] != str[i - 2]) {
            cnt = i - 1;
        }
    }
    if (str.size() - cnt == p) {
        str = spilt(cnt, str.size() - 1);
        return true;
    }
    return false;
}

int main() {

    int t_case;
    scanf("%d", &t_case);
    for (int i_case = 1; i_case <= t_case; i_case++) {
        cin >> str;
        while (check2() || check(8));
        while (check(7)) {
            while (check2() || check(8));
        }
        while (check(6)) {
            while (check(7)) {
                while (check2() || check(8));
            }
        }
        while (check(5)) {
            while (check(6)) {
                while (check(7)) {
                    while (check2() || check(8));
                }
            }
        }
        if (str.empty())
            puts("closed");
        else
            puts("open");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值