“浪潮杯”第九届山东省ACM大学生程序设计竞赛 A - Anagram[思维+字符串]

版权声明: https://blog.csdn.net/weixin_39792252/article/details/82148971

题目:给两个字符串A,B通过给定的操作,A->B;

感觉水题,瞎搞吧。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define INF 1e9+7;
using namespace std;
const int maxn = 100;

char a[maxn], b[maxn];

int stra(char x, char y) {
    if(y>=x) return (y-x);
    else return (y-'A'+'Z'-x+1);
}


int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%s%s", a, b) != EOF)
    {
        int len = strlen(a), ans = INF;
        sort(a, a+len);
        sort(b, b+len);

        for(int i = 0; i < len; i++) {
            int sum = 0;
            for(int j = 0; j < len; j++)
                    sum += stra(a[j], b[j]);
            ans = min(ans, sum);
            for(int j = len; j >= 0; j--) a[j] = a[j-1];
            a[0] = a[len];
        }
        printf("%d\n", ans);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/weixin_39792252/article/details/82148971