题目链接:https://vjudge.net/contest/162220#problem/D
题解:
1.构造两个函数(1-> 判断是否已经达到目的 2->计算达到目标字符串所需要多少步)//详见代码
2.主函数利用双重for循环来找到最少所需要的步数
AC code:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char str[110][110];int n;bool is_ok(char sta[] , char test[],int len){ for(int i=0;i<len;i++) { if(sta[i]!=test[i]) { return false; } }return true;}int step(int sta, int tes){ int cnt;int len; cnt=0; len=strlen(str[sta]);char test[2*len+1];for(int i=0;i<len;i++) { test[i]=str[tes][i]; }for(int i=0;i<len;i++) { test[len+i]=str[tes][i]; } test[2*len]='\0';while(cnt<len) { if(is_ok(str[sta],test+cnt,len)) { break; }else { cnt++; } }if(cnt>=len) return -1;else return cnt;}int main(){ int ans=99999999;scanf("%d",&n);for(int i=0;i<n;i++) { scanf("%s",str[i]); }for(int i=0;i<n;i++) { int cnt=0;for(int j=0;j<n;j++) { if(i!=j) { int temp=step(i,j);if(temp==-1) { printf("-1\n");return 0; }else { cnt+=temp; } } }if(ans>cnt) { ans=cnt; } }printf("%d\n",ans);return 0;}