Source Code
#include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	cin >> n;
	string s;
	cin >> s;
	int min = n;
	string ans = s;
	string temp;
	for (int i = 0; i < n; ++i){
		for (int j = 1; j <= n - i; ++j){
		    temp = s.substr(i, j);
			if(temp.length() < min && temp[0] == s[0] && temp[temp.length() - 1] == s[n - 1]){
			    ans = s.substr(i, j);
			    min = ans.length();
			}
		}
	}
	cout << n - min << endl;
	return 0;
}
Copy
Pop Pop Hiexy
GNU G++17
1100 ms
720 KB
Time Limit Exceeded