我是編程新手,我真的被困在了一個練習上。
我需要確定第一個字串中的每個字符是否可以唯一地替換為第二個字串中的一個字符。兩個字串的長度相等。
例如, "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.
如果有人有其他問題,請隨時提問。
uj5u.com熱心網友回復:
非優化解決方案示例
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")}");
}
}
}
演示
uj5u.com熱心網友回復:
在您的第一個 for 回圈中,結果將始終為 true,因為兩個 if 陳述句都將始終回傳 false。當 if 陳述句比較它們??的值時,“firstPhraseChars[i]”和“secondPhraseChars[j]”始終為空,因為在比較之前您從未將任何內容放入這些陣列中。
希望這會有所幫助,而不會放棄太多;)
uj5u.com熱心網友回復:
你可以找一個反例:如果你找到了,通訊員不存在:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/321797.html
