HDU 3363 冰糖葫芦

 

http://icpc.njust.edu.cn/Problem/Hdu/3363/

  • 首先是切的刀数是可穷举的:0、1、2
  • 其次是将其想象成圆环进行切割;
  • 计算两次切割位置时,用双重循环会超时。

 

#include 
using namespace std;

int main()
{
	int n;
	char c[100005];
	int	 cn[100005];
	while (cin >> n && n != 0) {
		int h= 0;
		int t = 0;
		cin >> c;
		if (n % 2 != 0) {
			cout << -1<< endl;
			continue;
		}
		else
		{
			for (int i = 0; i < n; i++) {
				if (c[i] == 'H') {
					h++;	
				}
				else
				{
					t++;
				}
				cn[i] = h;
			}
			if (h % 2 != 0 || t % 2 != 0) {
				cout << -1 << endl;
				continue;
			}
			int hh = 0;
			for (int i = 0; i < n / 2; i++) {
				if (c[i] == 'H') hh++;
			}
			if (hh == h / 2) {
				cout << 1 << "\n" << n / 2 << endl;
				continue;
			}
			else
			{
				cout << 2 << endl;
			}
			
			for (int i = 0; i < n / 2 - 1; i++) {
				if (cn[i + n / 2] - cn[i] == h/2) {
					cout << i+1 << " " << i + n / 2+1 << endl;
					break;
				}
			}
		}
	}
	return 0;
}

你可能感兴趣的:(HDU 3363 冰糖葫芦)