gpt4 book ai didi

c# - 两个字符串中的唯一字符对

转载 作者:行者123 更新时间:2023-12-03 21:15:23 24 4
gpt4 key购买 nike

所以我需要一些帮助来锻炼,我被卡住了!放轻松,我对此很陌生。练习如下:

Given two char arrays of equal lengths, determine if each character from the first array can be replaced uniquely with a character from the second one so that both arrays are equal. Display the character pairs between the two arrays.



示例 1:
给定以下输入: aabttd ,控制台将显示:
True
a => t
b => d

示例 2:
给定以下输入: abattd ,控制台将显示:
False

在第二个例子中,答案是 False因为字符 a 没有唯一的替换器: 两者 td对应。

我一直在思考这个问题,老实说,我觉得我什至不能再直接思考了。已经多次调整代码,我几乎不再理解它了。尽管如此,它仍然通过了第一个测试,但似乎在使用更长的字符串输入的测试中一直失败。例如输入: ala bala portocalacuc dcuc efghijcuc ,控制台显示 True然后是对,这是不对的。下面是我的 POS 代码,欢迎提供任何建议。
static void Main(string[] args)
{
string a = Console.ReadLine();
string b = Console.ReadLine();

string one = string.Empty;
string two = string.Empty;

bool res = false;
int count = 0;

for (int i = 0; i < a.Length; i++)
{
if (!one.Contains(a[i].ToString()))
{
if (!two.Contains(b[i].ToString()))
{
one += a[i];
two += b[i];
}
}
}

char[] firstPhrase = new char[one.Length];
char[] scndPhrase = new char[two.Length];

for(int i = 0; i < one.Length; i++)
{
bool temp = false;
for(int j = 0; j < two.Length; j++)
{
if(firstPhrase[j] != one[i])
{
for(int k = 0; k < scndPhrase.Length; k++)
{
if(scndPhrase[j] == two[i]) { res = true; break; }

}
if(res == true) { break; }
else { continue; }
}
if(firstPhrase[j] == one[i])
{
if (scndPhrase[j] == two[i]) { temp = true; continue; }
else { res = true; break; }
}

}
if(temp == false)
{
firstPhrase[count] = one[i];
scndPhrase[count] = two[i];
count++;
}
if(res == true) { break; }
}


if (res == true)
Console.WriteLine(res);
else
{
Console.WriteLine(!res);
for (int i = 0; i < firstPhrase.Length; i++)
{
Console.WriteLine($"{firstPhrase[i]} => {scndPhrase[i]}");
}
}
Console.Read();
}

我期待您的批评,并祝大家复活节快乐!

编辑 : 忘了说了,不好意思各位。测试不允许使用 LINQ 或任何其他指令,LINQ 实际上是我想尝试的第一件事。

最佳答案

这是仅使用 System 命名空间的解决方案。

这里的重要部分是首先将两个数组中的字符更改为标记。

这意味着,对于“ala bala portocala”,我们将拥有:

{ 0, 1, 0, 2, 3, 0, 1, 0, 2, 4, 5, 6, 7, 5, 8, 0, 1, 0 }

所以每当发现一个新字符(以前没有出现过)时,我们就将数字增加 1。

您可以看到,从这个有趣的角度来看,这可以很好地评估两个字符数组是否匹配。
static int[] Tokenize(char[] array)
{
int length = array.Length;
int[] distinctArray = new int[length];
int offset = 0;
for (int i = 0; i < length; i++)
{
bool appearedBefore = false;
for (int j = 0; j < i; j++)
{
if (array[j] == array[i])
{
appearedBefore = true;
distinctArray[i] = distinctArray[j];
break;
}
}
if (!appearedBefore)
{
distinctArray[i] = offset;
offset++;
}
}
return distinctArray;
}

然后是测试方法:
static void Test(char[] array1, char[] array2)
{
Console.WriteLine("[{0}] VS [{1}]", new string(array1), new string(array2));

int[] array1Tokenized = Tokenize(array1);
int[] array2Tokenized = Tokenize(array2);

for (int i = 0; i < array1.Length; i++)
{
if (array1Tokenized[i] != array2Tokenized[i])
{
Console.WriteLine("False");
return;
}
}

Console.Write("True");
for (int i = 0; i < array1.Length; i++)
{
bool appearedBefore = false;
for (int j = 0; j < i; j++)
{
if (array1[j] == array1[i])
{
appearedBefore = true;
break;
}
}
if (!appearedBefore && array1[i] != array2[i])
{
Console.Write(" {0} => {1}", array1[i], array2[i]);
}
}
Console.WriteLine();
}

结果:
static void Main(string[] args)
{
Test("ala bala portocala".ToCharArray(), "cuc dcuc efghijcuc".ToCharArray());

Test("ala bala portocala".ToCharArray(), "cuc dcuc efghfjcuc".ToCharArray());

Test("aab".ToCharArray(), "ttd".ToCharArray());
}

输出:
[ala bala portocala] VS [cuc dcuc efghijcuc]
False

[ala bala portocala] VS [cuc dcuc efghfjcuc]
True a => c l => u b => d p => e o => f r => g t => h c => j

[aab] VS [ttd]
True a => t b => d

关于c# - 两个字符串中的唯一字符对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61289048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com