forked from trannguyenhan/workspace-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchuoiguong.cpp
More file actions
39 lines (34 loc) · 858 Bytes
/
chuoiguong.cpp
File metadata and controls
39 lines (34 loc) · 858 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
/*Y tuong thuat toan duyet trau : Loai bo tung ki tu dau tien roi kiem tra doi xung cac ki tu con lai trong chuoi,
so ki tu bo di chinh la so ki tu can them vao de chuoi tro thanh chuoi guong*/
/*Han che : Van con chua dung toan bo test -> can cai tien de co thuat toan nhanh hon*/
bool checkDoiXung(string str)
{
if(str.length() == 1) return true;
else {
int i = 0;
int j = str.length() - 1;
while(i<j){
if(str[i] != str[j]) return false;
i++;
j--;
}
return true;
}
}
int makePalindrome(string str)
{
for(int i=0; i<str.length(); i++)
{
string sub = str.substr(i);
if(checkDoiXung(sub)) return i;
}
}
int main()
{
string str;
cin >> str;
cout << makePalindrome(str);
return 0;
}