日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

制作两个字符串字谜的最小步骤数

發布時間:2023/12/1 编程问答 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 制作两个字符串字谜的最小步骤数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Prerequisite:

先決條件:

  • Hashing data structure

    散列數據結構

Problem statement:

問題陳述:

Find the minimum number of steps to make two strings Anagram. Both strings are of the same length and the lower case. At each step, you can convert any character to string t to any other character.

找到使兩個字符串Anagram最少的步驟數。 兩個字符串的長度相同,并且都小寫。 在每個步驟中,您都可以將字符串t中的任何字符轉換為其他任何字符。

Example:

例:

Example 1: String s= "bba" String t= "aab"Minimum number of steps to make two strings anagram: 1 String t can be converted to "bab" which is anagram of string s="bba"Example 2: String s= "coding" String t= "coders"Minimum number of steps to make two strings anagram: 3 String t can be converted to "coding" which is anagram of string s="coding"(basically here we need to convert into same string)

Solution:

解:

We can solve the problem by using hashing. Two strings are said to be an anagram of each other if both have the same set of characters with equal frequency.

我們可以通過使用哈希解決問題。 如果兩個字符串都具有相同的頻率相同的字符集,則稱這兩個字符串為彼此的字謎。

Now since both of the string is of the same length it's often possible to convert the string t to the string s. We can solve this by using a hashing and greedy algorithm.
Firstly we calculate two hash tables for both the strings s & t to store the frequencies for each character.

現在,由于兩個字符串的長度相同,因此通常可以將字符串t轉換為字符串s 。 我們可以通過使用哈希和貪婪算法來解決此問題。
首先,我們為字符串s & t計算兩個哈希表,以存儲每個字符的頻率。

Let's say namely table1 and table2

比方說table1和table2

Both the table have 26 entries corresponding to 26 lowercase letters. The hash function that is used is: f(x)=x-'a' and instead of storing the characters itself we store the frequency like below:

兩個表都有26個對應于26個小寫字母的條目。 使用的哈希函數是:f(x)= x-'a',而不是存儲字符本身,我們存儲頻率如下:

  • Set table1[26]={0} to store frequencies for string s

    將table1 [26] = {0}設置為存儲字符串s的頻率

  • Set table2[26]={0} to store frequencies for string t

    將table2 [26] = {0}設置為存儲字符串t的頻率

  • for(int i=0;i<s.length();i++)table1[s[i]-'a']++;table2[t[i]-'a']++; end for
  • Now after this both the table have 26 characters('a' to 'z') which have values 0(in case the character doesn't exist) or non-zero. Now, as the strings are of equal length it's guaranteed that string t can be converted so that it becomes an anagram of string s.

    現在,在這之后,兩個表都包含26個字符(“ a”至“ z”),其值均為0(以防字符不存在)或非零。 現在,由于字符串長度相等,因此可以保證可以將字符串t轉換為字符串s的字謎。

    So for each character(index in table) there can be three cases

    因此,對于每個字符(表中的索引)可以有三種情況

    For i =0 to 26

    對于i = 0到26

  • table1[i]=table2[i]

    table1 [i] = table2 [i]

    Then no need to do anything as we need minimum no of conversion

    然后,無需做任何事情,因為我們需要最少的轉換次數

  • table2[i]>table1[i]

    table2 [i]> table1 [i]

    That means string

    這意味著字符串

    t has more number of same characters than in string s. Since in anagram both will the have same frequency for any character thus we need to convert the additional table2[i]-table1[i] to some other characters (not necessary to only one character)

    t具有比字符串s中更多的相同字符 由于在字謎中,兩個字符的頻率都相同,因此我們需要將附加的table2 [i] -table1 [i]轉換為其他字符(不必僅轉換為一個字符)

  • table2[i]<table1[i]

    table2 [i] <table1 [i]

    That means string

    這意味著字符串

    t has less number of same characters than in string s. Since in anagram both will the have same frequency for any character thus we have a deficiency of (table1[i]-table2[i]) number of characters which need to be converted from rest of the excess characters (which found in the second)

    t的相同字符數少于字符串s中的相同字符數 由于在字謎中,兩個字符的頻率都相同,因此我們缺少(table1 [i] -table2 [i])個字符,需要從其余多余字符(在第二個字符中找到)進行轉換

  • So what is the minimum number of steps required?

    那么,最少需要多少步?

    It's basically sum(table2[i]-table1[i]) if table2[i]-table1[i] Set Steps=0 For i=0 to 25If(table2[i]>table1[i])Steps+= table2[i]-table1[i] End forOr For i=0 to 25If(table1[i]>table2[i])Steps+= table1[i]-table2[i] End for

    Both algorithms will give the same result as both are anagrams of each other. But sticking to the question we are assuming that we are converting string t following the convention that if for any character the frequency in t ?is greater than s ?then we need to convert the extra characters which have deficiency string t.

    兩種算法的字謎結果都相同。 但是,堅持這個問題,我們假設要按照以下約定轉換字符串t :如果對于任何字符, t中的頻率大于s,則需要轉換具有不足字符串t的多余字符。

    #include <bits/stdc++.h> using namespace std;int minSteps(string s, string t) {int mymap1[26] = { 0 };int mymap2[26] = { 0 };for (int i = 0; i < s.length(); i++) {mymap1[s[i] - 'a']++;mymap2[t[i] - 'a']++;}int count = 0;for (int i = 0; i < 26; i++) {if (mymap2[i] > mymap1[i])count += mymap2[i] - mymap1[i];}return count; }int main() {cout << "Enter string, s:\n";string s;cin >> s;cout << "Enter string, t:\n";string t;cin >> t;cout << "Minimum number of steps needed : " << minSteps(s, t) << endl;return 0; }

    Output:

    輸出:

    RUN 1: Enter string, s: abb Enter string, t: baa Minimum number of steps needed : 1RUN 2: Enter string, s: coders Enter string, t: coding Minimum number of steps needed : 3

    翻譯自: https://www.includehelp.com/data-structure-tutorial/minimum-number-of-steps-to-make-two-strings-anagram.aspx

    總結

    以上是生活随笔為你收集整理的制作两个字符串字谜的最小步骤数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 日韩精品成人在线 | 少妇淫片| 色臀av| 综合久久影院 | 五月天激情视频在线观看 | 奇米在线视频 | 日韩av影音先锋 | 色综合99| 亚洲小说区图片区都市 | 91九色蝌蚪| 欧美日韩高清一区 | 成人在线天堂 | 牛牛澡牛牛爽一区二区 | 在线香蕉| 久青草资源福利视频 | 麻豆免费在线 | 亚洲综合色站 | www国产在线 | 老司机午夜在线 | 无码aⅴ精品一区二区三区 精品久久在线 | 日本不卡一区在线 | 欧洲精品久久一区二区 | 国产人妻大战黑人20p | 老女人丨91丨九色 | 国产精品视频久久久久 | 色网在线免费观看 | 日本黄色电影网址 | 亚洲人交配 | 大胸美女网站 | 亚洲欧美大片 | 中文字幕一区二区三区免费视频 | 亚洲人体在线 | 亚洲va在线∨a天堂va欧美va | 波多野吉衣在线视频 | 国产一级内谢 | 中文字幕一区在线播放 | 久久精品亚洲一区 | 欧美天堂在线 | 国产91欧美 | 精品无码黑人又粗又大又长 | av在线操 | 美女扒开尿口让男人捅爽 | 自拍偷拍亚洲视频 | 国产精品海角社区 | 97操| 一区二区三区视频网站 | 香蕉私人影院 | 视频在线播 | 黄色片地址| 嫩草视频在线观看免费 | 新狠狠干 | 97操碰 | 91精品久久久久久粉嫩 | 俺啪也 | 国产综合久久久久久鬼色 | 欧美日韩电影一区 | 懂色av懂色av粉嫩av分享吧 | 激情六月婷 | 国产剧情av在线 | 波波野结衣 | 狠狠爱综合 | 成人午夜电影网站 | 国产精品高潮呻吟久久aⅴ码 | 久久黄色片视频 | 毛片.com| 国产精品爽爽久久久久久 | 大吊一区二区三区 | 中文无码精品一区二区三区 | 亚洲激情在线观看视频 | 亚洲精品专区 | 国产第九页 | 玖玖国产精品视频 | 极品美妇后花庭翘臀娇吟小说 | 亚洲AV无码成人精品区在线观 | 午夜影院日本 | 黄色片xxx| 高清日韩av | 午夜亚洲天堂 | 贵族女沦为官妓h呻吟 | 国产综合亚洲精品一区二 | 国产在线视视频有精品 | 西西人体44www大胆无码 | 一女被多男玩喷潮视频 | 色婷婷国产精品久久包臀 | 91精品综合| 亚州男人的天堂 | 91蝌蚪网| 亚洲国产成人在线观看 | 亚洲精品 欧美 | 久久激情小说 | 久久久国际精品 | 亚洲AV无码精品黑人黑人 | 波岛野结衣 | 成人在线短视频 | 一区二区三区不卡在线观看 | 国产黄免费 | 激情三区 | 在线免费观看av不卡 | 一区二区麻豆 |