时间限制 内存限制
1000 ms 65536 KB

# 题目描述

给定两个长度相等的字符串 A,BA, B,每个字符串只包含小写字母。字符串 AA 和字符串 BB 的距离定义为每一位字符的 ASCII\text {ASCII} 值差的绝对值之和。例如, A = 'abc'B = 'bcd' ,则 AABB 的距离为:

ab+bc+cd=1+1+1=3|a - b| + |b - c| + |c - d| = 1 + 1 + 1 = 3

请编写一个程序,计算字符串 AABB 的距离。

# 输入格式

共两行,第一行代表字符串 AA,第二行代表字符串 BB。保证字符串只由小写字母组成,长度不超过 10001000

# 输出格式

一个正整数,表示字符串距离。

# 样例输入

abc
bcd

# 样例输出

3

# 题解:模拟

用两个字符数组存下字符串,直接依题意模拟即可。

参考代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
int ans;
char s1[1010], s2[1010];
int main()
{
    // freopen("A.in", "r", stdin);
    // freopen("A.out", "w", stdout);
    scanf("%s\n%s", s1, s2);
    int len = fmax(strlen(s1), strlen(s2));
    for (int i = 0; i < len; ++ i)
        ans += abs(s1[i] - s2[i]);
    
    printf("%d\n", ans);
    return 0;
}