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

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 。 我们可以通过使用哈希和贪婪算法来解决此问题。
首先,我们为字符串st计算两个哈希表,以存储每个字符的频率。

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',而不是存储字符本身,我们存储频率如下:

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

    将table1 [26] = {0}设置为存储字符串s的频率

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

    将table2 [26] = {0}设置为存储字符串t的频率

  3. 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

  1. table1[i]=table2[i]

    table1 [i] = table2 [i]

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

    然后,无需做任何事情,因为我们需要最少的转换次数

  2. 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]转换为其他字符(不必仅转换为一个字符)

  3. 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 25
If(table2[i]>table1[i])
Steps+= table2[i]-table1[i]
End for
Or
For i=0 to 25
If(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 : 1
RUN 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380329.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小学计算机教学教师培训,例谈小学信息技术课堂的有效教学

例谈小学信息技术课堂的有效教学在社会的各个领域&#xff0c;大家都不可避免地会接触到论文吧&#xff0c;论文可以推广经验&#xff0c;交流认识。为了让您在写论文时更加简单方便&#xff0c;以下是小编整理的例谈小学信息技术课堂的有效教学的论文相关内容&#xff0c;供大…

关于varchar2在pl/sql和schema级别的最大值

http://www.cublog.cn/u/30637/showart_1919003.html http://www.itpub.net/thread-1216548-1-1.html  转载于:https://www.cnblogs.com/wzc998/archive/2011/08/12/2136406.html

业务工作流平台设计(九)

自定义审核活动 前面已经讲了许多有关自定义活动在设计上需要注意的一些事项&#xff0c;但对于自定义审核活动来讲&#xff0c;我们的设计还要有许多工作要进行。 为了简化用户的流程上的设计将流程的一些算法封装到自定义活动中可以大大增加自定义活动的使用的方便性。其直接…

国王的魔镜

描述 国王有一个魔镜&#xff0c;可以把任何接触镜面的东西变成原来的两倍——只是&#xff0c;因为是镜子嘛&#xff0c;增加的那部分是反的。 比如一条项链&#xff0c;我们用AB来表示&#xff0c;不同的字母表示不同颜色的珍珠。如果把B端接触镜面的话&#xff0c;魔镜会把…

C和汇编---数组

0x01 初始化数组 1、没有初始化数组 #include "stdio.h" int main(void) {int data[4];for (int i0;i<4;i){printf("%d\t",data[i]);}return 0; }不同系统&#xff0c;输出结果可能不一样&#xff1a; 反汇编&#xff1a; 4: int data[4]; 5…

清华大学计算机组成与体系结构,清华大学出版社-图书详情-《计算机组成与体系结构(第2版)》...

作为“21世纪大学本科计算机专业系列教材”&#xff0c;遵照本系列教材评审组专家的意见&#xff0c;本书包括了数字电路基础、计算机组成、计算机体系结构3个部分内容。本书读者对象主要是学习计算机课程的大学生&#xff0c;包括计算机系的学生、软件学院的学生和非计算机专业…

txt如何单独单独选择一列_散列| 单独链接以解决冲突

txt如何单独单独选择一列Prerequisite: Hashing data structure 先决条件&#xff1a; 哈希数据结构 单独链接 (Separate chaining) In separate chaining, we maintain a linked chain for every index in the hash table. So whenever there is a Collison the linked list …

[我研究]Behavior Based Software Theft Detection - Hawk

Xinran Wang, Yoon-Chan Jhi, Sencun Zhu, Peng LiuPSU 背景&#xff1a; Behavior Based Software Theft Detection的系统包含四个步骤&#xff1a; 1、Dynamic Analysis - 产生System Call, Call Stack, Dependences 2、Noise Filtering 3、Extract SCDG Birthmarks 4、Compa…

整除个数

描述 1、2、3… …n这n(0< n<1000000000)个数中有多少个数可以被正整数b整除。 输入 输入包含多组数据 每组数据占一行&#xff0c;每行给出两个正整数n、b。 输出 输出每组数据相应的结果。 样例输入 2 1 5 3 10 4 样例输出 2 1 2 思路&#xff1a;一开始…

2020计算机二级题库第14PPT,计算机二级考试MSOffice考精彩试题库ppt操作题附问题详解.doc...

文档介绍&#xff1a;请在【答题】菜单下选择【进入考生文件夹】命令,并按照题目要求完成下面的操作。注意:以下的文件必须保存在考生文件夹下文慧是新东方学校的人力资源培训讲师,负责对新入职的教师进行入职培训,其PowerPoint演示文稿的制作水平广受好评。最近,她应北京节水展…

操作系统 系统开销比率_操作系统中的最高响应比率下一个(HRRN)调度

操作系统 系统开销比率操作系统中的HRRN调度是什么&#xff1f; (What is HRRN Scheduling in Operating System?) HRRN is the abbreviation of Highest Response Ratio Next Scheduling. It is an optimal scheduling algorithm. HRRN是最高响应率下一个调度的缩写 。 这是…

利用堆栈做循环

程序&#xff1a; #include "stdio.h" int main(int argc,char *argv[]) {char *str"%d";printf("hello world");__asm{ log:lea eax,logpush eaxlea ebx,strpush ebxpush eaxcall printfret 8}return 0;}运行&#xff1a;一直死循环运行下去 …

c# 多线程异步demo

一个 c# winform 多线程异步demo&#xff0c;分享下。 因为例子都很简单&#xff0c;所以不多说明&#xff0c;自己下载吧。转载于:https://www.cnblogs.com/chaobao/archive/2011/08/18/CSharpSync.html

(转)项目管理知识体系术语 123

项目管理知识体系术语(一)本词汇集包括和不包括的术语这个词汇集包括的词汇是&#xff1a;对于项目管理是唯一的&#xff0c;或接近于唯一的词汇(例如&#xff1a;范围说明&#xff0c;工作单元&#xff0c;工作分解结构&#xff0c;关键线路法)。对于项目管理是不是唯一的&…

矩形的个数

描述 在一个3*2的矩形中&#xff0c;可以找到6个1*1的矩形&#xff0c;4个2*1的矩形3个1*2的矩形&#xff0c;2个2*2的矩形&#xff0c;2个3*1的矩形和1个3*2的矩形&#xff0c;总共18个矩形。 给出A&#xff0c;B,计算可以从中找到多少个矩形。 输入 本题有多组输入数据&a…

计算机编程要哪方面天赋,编程要哪门子天赋

开局一张图写代码真的需要天赋吗&#xff1f;有句话是这样说的&#xff1a;论大家的努力程度&#xff0c;远不到拼天赋的时候。我认为所谓的天赋&#xff0c;应该是行业内Top10%水平才需要天赋&#xff0c;比如Linux缔造者Linus Torvalds&#xff0c;苹果发明者斯蒂夫沃兹尼亚克…

VBA之EXCEL删除和设置单元格行高等

‘删除Sheet1上的单元格区域A1:D10,并将其余单元格左移以填补被删除单元格的位置 Sheet1.Range(“A1:D10”).Delete Shift:xlShiftToLeft ‘删除指定行 Range(“1:1”).Delete ‘删除指定列 Columns(5).Delete ‘删除当前行 ActiveCell.EntireRow.Delete ‘删除工作表中的重复行…

java void方法_Java对象类的最终void wait(long ms)方法,包含示例

java void方法对象类最终无效等待(长毫秒) (Object Class final void wait(long ms)) This method is available in java.lang.Object.wait(long ms). 此方法在java.lang.Object.wait(long ms)中可用。 This method causes the current thread to wait for a specified amount …

C和汇编----字符串

字符串是以空字符&#xff08;\0&#xff09;结尾的char类型数组。 0x01 定义字符串和初始化 用双引号括起来的内容称为字符串字面量&#xff0c;也叫字符串常量&#xff0c;双引号中的字符串和编译器自动加入\0字符&#xff0c;都作为字符串存储在内存中 #include "st…

出现此版本的sql server不支持用户实例登陆标志 问题的解决方法

场景: 开发工具vs2005 数据库 sql 2005加入了Enterprise Libraty后,新增了一个data access application block,在写ConnectString的时候,出现这个错误提示,解决办法:在连接属性的设置里边,点高级,将User Instance 设置为false,默认的true,所以才导致这个问题的出现.设置好后,重…