ZOJ-1262

小模拟一道,这题我是直接硬搞的,擦在快TLE的边缘过了,小偷了个懒。。正确做法应该是找出循环的规律再求解,因为如果s很大的话直接循环s次求解是必挂的。。另外字符只有a和b两种情况,因此可以优化成bool数组,节省不少空间

#include<iostream>
#include<string>
#include<vector>
#include<cstring>
#include<deque>

using namespace std;

namespace
{
	int number(bool b1, bool b2, bool b3)
	{
		int res = b1;
		res = res * 2 + b2;
		return res * 2 + b3;
	}

	bool compare(const deque<bool> &q1, const deque<bool> &q2)
	{
		for (size_t i = 0; i < q1.size(); i++)
			if (q1[i] != q2[i])
				return q1[i];
		return false;
	}

	deque<bool> maxq(deque<bool> &q)
	{
		deque<bool> res = q;
		int count = q.size() - 1;
		while (count--)
		{
			bool head = q.front();
			q.pop_front();
			q.push_back(head);
			if (compare(q, res))
				res = q;
		}
		return res;
	}
}

int main()
{
	int n, m;
	string s, rule;
	bool map[8], src[16], temp[16];
	deque<bool> Q;
	while (cin >> n)
	{
		cin >> s;
		for (int i = 0; i < n; i++)
			src[i] = s[i] == 'a';

		for (int i = 0; i < 8; i++)
		{
			cin >> rule;
			map[number(rule[0] == 'a', rule[1] == 'a', rule[2] == 'a')] =
					rule[3] == 'a';
		}
		int left, right;
		cin >> m;
		while (m--)
		{
			for (int i = 0; i < n; i++)
			{
				left = (i + n - 2) % n;
				right = (i + 1) % n;
				temp[i] = map[number(src[left], src[i], src[right])];
			}
			memcpy(src, temp, sizeof(temp));
		}
		Q.clear();
		for (int i = 0; i < n; i++)
			Q.push_back(src[i]);
		Q = maxq(Q);
		for (size_t i = 0; i < Q.size(); i++)
			cout << (Q[i] ? 'a' : 'b');
		cout << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值