USACO 1.3.3 – Calf Flac


int main() {
	ofstream fout("calfflac.out");
	ifstream fin("calfflac.in");
	int i, a, b, best = 0, best_a, best_b, l, b_l, a_l, spoiled;
	string s;
	char line[3000];
	while (fin.getline(line, 3000))
		s += string(line) + "\n";

	for (i = 0; i < s.size(); i++) {
		a = b = a_l = b_l = i, l = 0;
		while (a >= 0 && b < s.size()) {
			if (!isalpha(s[a]) || !isalpha(s[b])) {
				a -= !isalpha(s[a]), b += !isalpha(s[b]);
				continue;
			}
			if (tolower(s[a--]) != tolower(s[b++]))
				break;
			else
				a_l = a, b_l = b, l += 1 + (a + 1 != b - 1);
		}
		if (l > best)
			best = l, best_a = a_l, best_b = b_l;
	}

	fout << best << endl;
	if (best)
		fout << s.substr(best_a + 1, best_b - best_a - 1) << endl;
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: