等价字符串
有两个长度相同的字符串,均由字母A-Z构成,长度不超过100。请判断是否可以把其中一个字符串的各个字母重排,然后对26个字母做一个一一映射,使得两个字符串相同。如JWPUDJSTVP重排后可以得到WJDUPSJPVT,然后把每个字母映射到它前一个字母(B->A, C->B, ..., Z->Y, A->Z),得到VICTORIOUS。
输入
两个字符串,以空格分隔。
输出
若可以请输出YES,否则输出NO。
示例输入
ABBCFEA
CCGGHJB
示例输出
YES
我的错误程序:
#include <stdio.h>
#include <string.h>
int main() {
char a, b;
int t = 0;
int p = 0;
int m = 0;
while (scanf("%c%c\n", a, b) == 2 && getchar() != '\n' ) {
strlen(a) == strlen(b);
int n = strlen(a);
for (int i = 0; i < n; i++) {
int t = a;
if (a < a) {
t = a;
a = a;
a = t;
}
}
for (int j = 0; j < n; j++) {
int p = b;
if (b < b) {
p = b;
b = b;
b = p;
}
}
for (int i = 0, j = 0; i < n, j < n; i++, j++) {
if (a > b) {
if (a == 'A')
a == 'Z';
else
a = a - 1;
if (a == b)
m = 1;
else {
m = 0;
break;
}
} else if (b == 'A')
b == 'Z';
else
b = b - 1;
if (a == b)
m = 1;
else {
m = 0;
break;
}
}
if (m = 1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
} 本帖最后由 yuxijian2020 于 2021-3-28 00:26 编辑
你这个示例输入和输出,我手算都算不出来YES....
#include <stdio.h>
#define bool int
#define true 1
#define false 0
bool EqualString(char a, char b)
{
if (a == 0 && b == 0)
return true;
else if ((a == 0 && b != 0) || (a != 0 && b == 0) || strlen(a) != strlen(b))
return false;
char hash_a;
char hash_b;
bool isBreak = false;
memset(hash_a, 0, 26);
memset(hash_b, 0, 26);
for (int i = 0; i < 100; ++i)
{
char temp;
if (a == 'A')
temp = 'Z';
else
temp = a - 1;
hash_a++;
hash_b - 0x41]++;
}
for (int i = 0; i < 26; ++i)
{
if (hash_a != hash_b)
{
isBreak = true;
break;
}
}
if (!isBreak)
return true;
memset(hash_a, 0, 26);
memset(hash_b, 0, 26);
for (int i = 0; i < 100; ++i)
{
char temp;
if (b == 'A')
temp = 'Z';
else
temp = b - 1;
hash_b++;
hash_a - 0x41]++;
}
for (int i = 0; i < 26; ++i)
{
if (hash_a != hash_b)
return false;
}
return true;
}
int main()
{
char a = "WJDUPSJPVT";
char b = "VICTORIOUS";
bool flag = EqualString(a, b);
if (flag)
printf_s("YES\n");
else
printf_s("NO\n");
return 0;
} 贴个运行成功的图
页:
[1]