【解決方法】この質問について教えてください


Input to your program are 3 strings S1 S2 and S3 of lower case alphabets and no spaces or special characters. S1 and S2 are the same size. Here are the constraints on S1 and S2:
In string S1 and S2 the alphabets at the same index can be replaced with each other.
If alphabet p can be replaced with q then q can also be replaced with p
If alphabet p can be replaced with alphabet q , and the alphabet q can be replaced with alphabet r then alphabet p can also be replaced with r.
1 <  length of strings  S1,S2,S3 < 999999
length of string S1 = length of string S2
All the strings consist of lowercase English letters.
 
Ex : You are given two strings:
S1−pqr
S2−zrg
 
Here, the alphabet p can be replaced with alphabet z, alphabet q can be replaced with r, and alphabet r with g. The alphabet q can also be replaced with g according to the 3rd rule above.
 
Definition of lexicographical sorting https://en.wikipedia.org/wiki/Lexicographic_order#:~:text=In%20mathematics%2C%20the%20lexicographic%20or,of%20a%20totally%20ordered%20set.
 
Input format
First line: String S1
Second line: String S2
Third line: String S3
Output format
You can replace any alphabet of S3 with any of these alternatives based on the properties learned from S1 and S2. By doing so you can construct many such new strings. Out of all these strings your program should output the smallest string assuming they are sorted lexicographically.
 
Sample Input
dcba
edcb
decb
Output
aaaa

私が試したこと:

私は試しましたが、答えを得ることができませんでした

解決策 1

私たちは立ち往生している人々を喜んで助けますが、それは私たちがあなたのためにすべてをするためにここにいるという意味ではありません! 私たちがすべての作業を行うことはできません。あなたはこれに対して報酬を受け取っているか、またはそれはあなたの成績の一部であり、私たちがあなたのためにすべてを行うことはまったく公平ではありません.

だから私たちはあなたが仕事をする必要があり、あなたが行き詰まったときにあなたを助けます. それは、あなたが提出できる段階的な解決策を提供するという意味ではありません!
現在の状況と、プロセスの次のステップを説明することから始めます。 次に、その次のステップを機能させるために何を試みたか、またその際に何が起こったかを教えてください。

開始するのに問題がある場合は、これが役立つ場合があります。 問題を解決するためのコードの書き方、初心者向けガイド[^]

解決策 4

4 つのステップで可能な手順を次に示します。

C++
string s1("dcba");  //  first line
string s2("edcb");  //  second line 
string s3("decb");  //  third line

// 1. Combine the two strings: s1="dcbaedcb", s2="edcbdcba"

unsigned len = s1.length();

// 2. search lowest match
for (unsigned i = 0; i < len; i++) {
  for (unsigned k = i+1; k < len; k++) {
     if (s2[i] == s1[k]) {
     ...
     }
  }
}

// 3. delete duplicates

//     original:  append:     lowest:     del_dup:
// s1: dcba       dcba+edcb   dcbaedcb	  abcde
// s2: edcb       edcb+dcba   aaaaaaaa	  aaaaa

// 4. Generate and output result: aaaa
for (unsigned i=0; i < s3.length(); i++) {
  ...
  }

cout << "Result: " << s3 << "\n";

解決策 2

ご覧のとおり、ソートされていない方法で指定された 3 つの文字列があるため、最初に文字列をソートし、文字列 1 と文字列 2 を比較してから、どちらが小さいかを比較して、その文字列を s3 と比較し、s1 の場合

解決策 3

コードは、上記の質問の「試したこと」の下にコピーする必要があります。 ただし、これは動作する Python コードではありません。 C/C++ で起こりうる問題について、このフォーラムで支援したいと考えています。 ここでは翻訳サービスは提供されていません。

このコードは、それがどこかにコピーされたことを示しており、実行されたことはなく、オリジナルの作品はまったく含まれていません。

Python
def recheck(dp):
flag=0
for i in dp.keys():
if dp[dp[i]] <dp[i]:
dp[i]="dp[dp[i]]
" flag+="1
" if(flag="=0):
" return
="" else:
="" recheck(dp)

s1="input()
s2=input()
s3=input()
dp={}
for" i="" in="" range(97,123):
="" dp[chr(i)]="chr(i)
"
for="" range(len(s1)):
="" if(s1[i]<="s2[i]):
" dp[s2[i]]="s1[i]
" elif(s1[i]="">s2[i]):
dp[s1[i]]=s2[i]
recheck(dp)

私はPythonを使用してこの質問をしましたが、それも機能しますが、c ++を使用してそれを行うことはできません

少なくとも一見すると、コードは初心者でもそれほど複雑ではありません。
実装に問題がある場合は、こちらで喜んで話し合います。

コメント

タイトルとURLをコピーしました