簡體   English   中英

C#中字符的唯一對應關系

[英]Unique correspondence for characters in C#


我是編程新手,我真的被困在了一個練習上。
我需要確定第一個字符串中的每個字符是否可以唯一地替換為第二個字符串中的一個字符。 兩個字符串的長度相等。
例如, "aabc ea" 和 "ddtf hd" ,結果需要是:
 True a => d b => t c => f => e => h

如果我有例如 "abac ea" 和 "ddtf hd" ,結果需要是:

錯誤的

由於“abac ea”和“ddt hd”沒有唯一的替代品。

這是我的代碼:

 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(); } } }

有人可以幫我理解我做錯了什么嗎? 我不知道了,我覺得這段代碼永遠行不通。 需要有一些解決方案,我不理解。

我不應該使用 LINQ、列表或字典,只能使用 System.
如果有人有其他問題,請隨時提問。

非優化解決方案示例

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")}");
        }
    }
}

演示

你可以找一個反例:如果你找到了,通訊員不存在:

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;
}

如果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;
}

如果您正在尋找一對一的對應關系,您只需檢查兩次

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

在您的第一個 for 循環中,結果將始終為 true,因為兩個 if 語句都將始終返回 false。 當 if 語句比較它們​​的值時,“firstPhraseChars[i]”和“secondPhraseChars[j]”始終為空,因為在比較之前您從未將任何內容放入這些數組中。

希望這會有所幫助,而不會放棄太多;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM