O(n^2)算法可过
查看原帖
O(n^2)算法可过
195066
Loxilante楼主2023/11/16 17:30

代码如下,用了O(n^2)找连消BTTB序列:

#include <bits/stdc++.h>
using namespace std;
const int U = 1e5+50;
int main(void)
{
	freopen("D:\data.txt", "r", stdin);
	
	int T; cin>>T;
	while(T--)
	{
		int n;
		string str;
		cin>>n>>str;
		
		bitset<U> begin, end;
		for(int i = 1; i < n; i++)
		{
			if (str[i-1] == 'B' && str[i] == 'T')
			{
				int j = i+2, no = 0;
				while(str[j-1] == 'B' && str[j] == 'T') j += 2;
				if (!(str[j-1] == 'T' && str[j] == 'B')) no = 1;
				while(str[j-1] == 'T' && str[j] == 'B') j += 2;
				if (!no)
				{
					begin[i-1] = 1;
					end[j-2] = 1;
				}
			}
		}
		
		int cnt = 0, ans = 0;
		str += 'C';
		for(int i = 0; i <= n; i++)
		{
			if (str[i] == 'T') cnt++;
			else
			{
				if (i-cnt-1 >= 0 && end[i-cnt-1]) cnt++;
				if (begin[i]) cnt++;
				ans = max(cnt, ans);
				cnt = 0;
			}
		}
		cout<<ans<<endl;
	}
}

这么好卡不知道出题人为什么没卡o.o

#include <bits/stdc++.h>
using namespace std;
int main(void)
{
	freopen("D:\\data.txt", "w", stdout);
	
	cout<<5<<endl;
	for(int i = 0; i < 5; i++)
	{
		cout<<100000<<endl;
		for(int j = 0; j < 25000; j++) cout<<"BT";
		for(int j = 0; j < 25000; j++) cout<<"TB";
		cout<<endl;
	}
}
2023/11/16 17:30
加载中...