Skip to content

Instantly share code, notes, and snippets.

@harurunrunrun
Created October 25, 2021 05:06
Show Gist options
  • Save harurunrunrun/3ab33fa147410578f05094913986fd8c to your computer and use it in GitHub Desktop.
Save harurunrunrun/3ab33fa147410578f05094913986fd8c to your computer and use it in GitHub Desktop.
ICPC模擬国内予選2021 D
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
using ll=long long;
string S;
void solve(){
int N=S.size();
ll ans=0;
for(int i=0;i<N/2;i++){
if(S.at(i)==S.at(N-i-1)){
continue;
}
bool c=true;
for(int j=i;j<N-i-1;j++){
if(S.at(j)==S.at(N-i-1)){
int l=j-i+1;
bool f=true;
for(int k=0;k<l;k++){
if(S.at(i+k)!=S.at(N-i-l+k)){
f=false;
break;
}
}
if(f){
c=false;
ans+=l*l;
for(int k=0;k<l;k++){
S.at(i+k)=S.at(N-i-1-k);
}
break;
}
}
}
if(c){
cout<<-1<<endl;
return;
}
}
cout<<ans<<endl;
return;
}
int main(){
while(true){
cin>>S;
if(S=="#"){
break;
}
solve();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment