https://vpn.bupt.edu.cn/http/10.105.242.80/problem/p/90/
自己写得low代码
#include <bits/stdc++.h>
#define FF(a,b) for(int a=0;a<b;a++)
#define F(a,b) for(int a=1;a<=b;a++)
#define LEN 510000
#define INF 1000000
#define bug(x) cout<<#x<<"="<<x<<endl;
using namespace std;
typedef long long ll;
int dist[30][30];
int calc_dist(int a,int b){
int n1=min(a,b);
int n2=max(a,b);
return min(n2-n1,n1+26-n2);
}
int main()
{
// freopen("./in","r",stdin);
FF(i,26){
FF(j,26){
dist[i][j]=calc_dist(i,j);
}
}
int N;
scanf("%d",&N);
while(N--){
char buf[1010];
int num[1010];
int used[26]={0};
scanf("%s",buf);
FF(i,strlen(buf))
num[i]=buf[i]-'a';
int minSum=INF;
FF(i,strlen(buf)){
int t=num[i];
if(used[t]) continue;
used[t]=1;
int tmpSum=0;
FF(j,strlen(buf)){
int u=num[j];
int d=dist[t][u];
tmpSum+=d;
}
if(tmpSum<minSum)
minSum=tmpSum;
}
printf("%d\n",minSum);
}
return 0;
}
大佬的代码
https://blog.csdn.net/birdstorm_s/article/details/20306145?utm_source=blogxgwz9
/*
USER_ID: test#birdstorm
PROBLEM: 90
SUBMISSION_TIME: 2014-02-26 17:41:52
*/
#include <stdio.h>
#define For(i,m,n) for(i=m;i<n;i++)
#define MIN(x,y) (x)<(y)?(x):(y)
#define INF 1e9
#define MAXN 1005
int cnt[30];
char s[MAXN];
main()
{
int i, j, T, min, sum, len, x;
scanf("%d",&T);
while(T--){
For(i,0,26) cnt[i]=0;
min=INF;
scanf("%s",s);
for(i=0;s[i];i++) cnt[s[i]-'a']++;
For(i,0,26){
sum=0;
For(j,0,26){
if(i>j) x=MIN(i-j,26-(i-j));
else x=MIN(j-i,26-(j-i));
sum+=cnt[j]*x;
}
min=MIN(min,sum);
}
printf("%d\n",min);
}
return 0;
}