poj3617 Best Cow Line

/*比较需要注意的是这句话*
  Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.
  没有特殊处理格式的话,就很容易导致PE
*/

#include <iostream>
using namespace std;
int N;
const int MAX_N = 2005;
char S[MAX_N + 1];

void solve()
{
	//剩余字符串S[i]中,i的取值范围为[a,b] 
	int a = 0, b = N - 1;
	int ans = 0;
	while (a <= b)
	{
		//将从左起和从右起的字符串比较
		bool left = false;
		for (int i = 0; a + i <= b; i++)
		{
			if ( S[a + i] < S[b - i] )
			{
				left = true;
				break;
			}
			else if ( S[a + i] > S[b - i] )
			{
				left = false;
				break;
			}
		}
		if (left) cout << S[a++];
		else cout << S[b--];
		ans++;
		if (ans % 80 == 0 && ans < N - 1) cout << endl; 
	}
	cout << endl;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++) cin >> S[i]; 
	solve();
	return 0;
}

posted @ 2017-08-24 19:40  mofushaohua  阅读(103)  评论(0编辑  收藏  举报