Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fast()(ios_base::sync_with_stdio(false),cin.tie(NULL));
- int ind=-1;
- bool ftest(string s)
- {
- int l=s.size();
- int i=0,j=l-1,cnt=0;
- while(i<j)
- {
- if(s[i]!=s[j])
- {
- cnt++;
- if(s[i]==s[j-1])
- {
- ind =j;
- j--;
- }
- else if(s[i+1]==s[j])
- {
- ind=i;
- i++;
- }
- }
- i++,j--;
- if(cnt>1)break;
- }
- if(cnt==0 || cnt==2)return false;
- return true;
- }
- bool stest(string s)
- {
- int l=s.size();
- int i=0,j=l-1,cnt=0;
- while(i<j)
- {
- if(s[i]!=s[j])
- {
- cnt++;
- if(s[i+1]==s[j])
- {
- ind =i;
- i++;
- }
- else if(s[i]==s[j-1])
- {
- ind=j;
- j--;
- }
- }
- i++,j--;
- if(cnt>1)break;
- }
- if(cnt==0 || cnt==2)return false;
- return true;
- }
- int main()
- {
- fast();
- int t;
- cin>>t;
- while(t--)
- {
- string s;
- cin>>s;
- if(ftest(s))cout<<ind<<endl;
- else if(stest(s))cout<<ind<<endl;
- else cout<<"-1"<<endl;
- }
- }
Add Comment
Please, Sign In to add comment