码迷,mamicode.com
首页 > 其他好文 > 详细

某场CF B Octagons

时间:2015-08-07 11:07:08      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:模拟

题目: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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

某场CF B Octagons

标签:模拟

原文地址:http://blog.csdn.net/u014357885/article/details/47335699

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!