-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome-index.cpp
More file actions
63 lines (50 loc) · 893 Bytes
/
Copy pathpalindrome-index.cpp
File metadata and controls
63 lines (50 loc) · 893 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void palindromeIndex(string s)
{
int first = 0, copyFirst = 0, last = s.length() - 1, copyLast = 0;
int result = -1;
int found = 0;
for (int i = 0; i < s.length()/2; ++i)
{
if (s[first] != s[last] && first < last)
{
if (found)
{
result = copyFirst + copyLast;
break;
}
if (s[first+1] == s[last])
{
copyLast = last;
result = first;
found++, first++;
}
else if (s[first] == s[last -1])
{
copyFirst = first;
result = last;
found++, last--;
}
}
first++, last--;
}
cout<<result<<endl;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T;
cin >> T;
while(T)
{
string input;
cin>>input;
palindromeIndex(input);
T--;
}
return 0;
}