Created
April 8, 2019 15:26
-
-
Save luciocf/7b3bc85c888e4d64d5f38f00cb1432a0 to your computer and use it in GitHub Desktop.
Noic - Iniciante - Semana 51 - Problema 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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