Skip to content

Instantly share code, notes, and snippets.

@luciocf
Created April 8, 2019 15:26
Show Gist options
  • Save luciocf/7b3bc85c888e4d64d5f38f00cb1432a0 to your computer and use it in GitHub Desktop.
Save luciocf/7b3bc85c888e4d64d5f38f00cb1432a0 to your computer and use it in GitHub Desktop.
Noic - Iniciante - Semana 51 - Problema 1
// Noic - Iniciante - Semana 51 - Problema 1
// Complexidade: O(n^3)
#include <bits/stdc++.h>
using namespace std;
string s;
bool check(int a, int b)
{
// substring de [a..b] e sua reversa
string S, R;
for (int i = a; i <= b; i++)
{
S.push_back(s[i]);
R.push_back(s[i]);
}
reverse(R.begin(), R.end());
for (int i = 0; i < S.size(); i++)
if (S[i] != R[i])
return false;
return true;
}
int main(void)
{
cin >> s;
int ans = 0;
for (int i = 0; i < s.size(); i++)
{
for (int j = i; j < s.size(); j++)
{
if (check(i, j))
{
// se a substring [i..j] for um palíndromo,
// atualizamos nossa resposta
ans = max(ans, j-i+1);
}
}
}
cout << ans << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment