[Algorithm] Palindrome

Palindrome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
int Palindrome(const string &s){
int ret = 1;
int len;
for(int i=0;i<s.length()-1;i++){
len = 0;
while((i-len-1)>=0&&(i+len+1<s.length()) && s[i-len-1]==s[i+len+1]) ++len;
ret = ret > (len<<1)+1 ? ret : (len<<1)+1;

if(s[i]!=s[i+1])
continue;
len = 1;
while((i-len)>=0&&(i+len+1<s.length()) && s[i-len]==s[i+1+len]) ++len;
ret = ret > len<<1 ? ret : len<<1;
}
return ret;
}

int main(int argc, char** argv){
const string s = "BANANA";
cout<<Palindrome(s)<<endl;
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/21/Algorithm/palindrome/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.