简体   繁体   English

C#中字符的唯一对应关系

[英]Unique correspondence for characters in C#


I'm new to programming and I got really stuck on an exercise. 我是编程新手,我真的被困在了一个练习上。
I need to determine whether each character in the first string can be uniquely replaced by a character in the second string. 我需要确定第一个字符串中的每个字符是否可以唯一地替换为第二个字符串中的一个字符。 Both strings are equal in length. 两个字符串的长度相等。
For example, "aabc ea" and "ddtf hd" , the result needs to be: 例如, "aabc ea" 和 "ddtf hd" ,结果需要是:
 True a => d b => t c => f => e => h

If I have for example "abac ea" and "ddtf hd" , the result needs to be:如果我有例如 "abac ea" 和 "ddtf hd" ,结果需要是:

False错误的

Since "abac ea" and "ddt hd" don't have an unique replacement.由于“abac ea”和“ddt hd”没有唯一的替代品。

This is my Code:这是我的代码:

 using System; namespace UniqueStrings { class Program { static void Main(string[] args) { string firstPhrase = Console.ReadLine(); string secondPhrase = Console.ReadLine(); bool result = false; int charsCount = 0; char[] firstPhraseChars = new char[firstPhrase.Length]; char[] secondPhraseChars = new char[secondPhrase.Length]; if (firstPhrase.Length != secondPhrase.Length) { result = false; } for (int i = 0; i < firstPhrase.Length; i++) { if (firstPhrase[i] == firstPhraseChars[i]) { firstPhraseChars[i] = firstPhrase[i]; secondPhraseChars[i] = secondPhrase[i]; } for (int j = 0; j < secondPhrase.Length; j++) { if (secondPhrase[j] == secondPhraseChars[j]) { firstPhraseChars[j] = firstPhrase[j]; secondPhraseChars[j] = secondPhrase[j]; result = false; } else { result = true; } } } for (int i = 0; i < firstPhrase.Length; i++) { if (result == false) { firstPhraseChars[charsCount] = firstPhrase[i]; secondPhraseChars[charsCount] = secondPhrase[i]; charsCount++; } } if (result == false) Console.WriteLine(result); else { Console.WriteLine(result); for (int i = 0; i < firstPhrase.Length; i++) { Console.WriteLine(firstPhrase[i] + " => " + secondPhrase[i]); } } Console.Read(); } } }

Can someone please help me understand what I'm doing wrong?有人可以帮我理解我做错了什么吗? I have no idea anymore and I feel like this code will never work.我不知道了,我觉得这段代码永远行不通。 There needs to be some solution to this, which I'm not understanding.需要有一些解决方案,我不理解。

I'm not supposed to use LINQ, list or dictionary, only System.我不应该使用 LINQ、列表或字典,只能使用 System.
If anyone has other questions, feel free to ask.如果有人有其他问题,请随时提问。

exemple of non-optimized solution非优化解决方案示例

using System;

namespace UniqueStrings
{
    class Program
    {
        static bool CheckStringSimilarity(string firstPhrase, string secondPhrase)
        {
            if (firstPhrase.Length != secondPhrase.Length)
            {
                return false;
            }

            var length = firstPhrase.Length;
            for (var i =0; i<length; i++)
            {
                for(var j=0; j<length; j++ )
                {
                    if((firstPhrase[i] == firstPhrase[j]) && (secondPhrase[i] != secondPhrase[j]))
                    {
                       return false;                       
                    }
                    if((firstPhrase[i] != firstPhrase[j]) && (secondPhrase[i] == secondPhrase[j]))
                    {
                       return false;                       
                    }                   
                }
            }

            return true;
        }
        static void Main(string[] args)
        {

            Console.WriteLine($"CheckStringSimilarity('aaa','bbb') = {CheckStringSimilarity("aaa", "bbb")}");
            Console.WriteLine($"CheckStringSimilarity('aaab','bbbc') = {CheckStringSimilarity("aaab", "bbbc")}");
            Console.WriteLine($"CheckStringSimilarity('rrt','aze') = {CheckStringSimilarity("rrt", "aze")}");
            Console.WriteLine($"CheckStringSimilarity('rrt dd','aad aa') = {CheckStringSimilarity("rrt dd", "aad aa")}");
        }
    }
}

DEMO演示

You can look for a counter example : if you've found it, correspondent doesn't exist:你可以找一个反例:如果你找到了,通讯员不存在:

private static bool HasCorrespondence(string left, string right) {
  if (left == null)
    return right == null;
  if (right == null)
    return false;

  if (left.Length != right.Length)
    return false;

  // known correspondence
  Dictionary<char, char> correspondence = new Dictionary<char, char>();

  for (int i = 0; i < left.Length; ++i)
    if (correspondence.TryGetValue(left[i], out char expected)) {
      // counter example: we want expected, but have right[i]
      if (expected != right[i])
        return false;
    }
    else
      // we have nothing for left[i], so we can add (left[i], right[i]) pair   
      correspondence.Add(left[i], right[i]);

  // no counter example exists, return true
  return true;
}

If Dictionary is prohibited, you can emulate it with help of array:如果Dictionary被禁止,你可以在数组的帮助下模拟它:

private static bool HasCorrespondence(string left, string right) {
  if (left == null)
    return right == null;
  if (right == null)
    return false;

  if (left.Length != right.Length)
    return false;

  int[] correspondence = new int[char.MaxValue];

  for (int i = 0; i < left.Length; ++i)
    if (correspondence[left[i]] != 0) {
      if (correspondence[left[i]] != right[i])
        return false;
    }
    else
      correspondence[left[i]] = right[i];

  return true;
}

If you are looking for one to one correspondence, you can just check twice "如果您正在寻找一对一的对应关系,您只需检查两次

private static bool HasOneToOne(string left, string right) =>
  HasCorrespondence(left, right) &&
  HasCorrespondence(right, left);

In your first for loop the result will always be true since both if statements will always return false.在您的第一个 for 循环中,结果将始终为 true,因为两个 if 语句都将始终返回 false。 When the if statements compare their values "firstPhraseChars[i]" and "secondPhraseChars[j]" are always empty since you never put anything into these arrays before the comparison.当 if 语句比较它们​​的值时,“firstPhraseChars[i]”和“secondPhraseChars[j]”始终为空,因为在比较之前您从未将任何内容放入这些数组中。

Hope this helps without giving away too much ;)希望这会有所帮助,而不会放弃太多;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM