Source Code
#include <iostream>
using namespace std;


int main(){
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
	
	int n, ans = 0;
	string x;
	cin >> n;
	cin >> x;
	const int l = x[0], k = x[n-1];
	int m = n-1;
	int i = 0;
	while (1)
	{
		bool flag1 = 0, flag2 = 0;
		if (i == m)
		{
			break;
		}
		if (x[i+1] == l && x[m] == k)
		{
			++ans;
			i++;
			flag1 = 1;
			continue;	
		}
		if (x[i] == l && x[m-1] == k)
		{
			++ans;
			m--;
			flag2 = 1;
			continue;
		}
		if (flag1 == 0 && flag2 == 0)
		{
			break;
		}
		
	}
	
	cout << ans;

	return 0;
}

Copy
Pop Pop microwave6
GNU G++17
0 ms
416 KB
Wrong Answer
Test Case #1
0 ms
360 KB
Accepted
Input
5
11100
Output
3
Judge Output
3
Checker Message
"3"
Test Case #2
0 ms
416 KB
Wrong Answer
Input
10
1100010011
Output
2
Judge Output
9
Checker Message
1st words differ - expected: '9', found: '2'