博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
制作两个字符串字谜的最小步骤数
阅读量:2531 次
发布时间:2019-05-11

本文共 4734 字,大约阅读时间需要 15 分钟。

Prerequisite:

先决条件:

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: 1String 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: 3String 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

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=0For i=0 to 25	If(table2[i]>table1[i])	Steps+= table2[i]-table1[i]End forOrFor 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 
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:abbEnter string, t:baaMinimum number of steps needed : 1RUN 2:Enter string, s:codersEnter string, t:codingMinimum number of steps needed : 3

翻译自:

转载地址:http://obazd.baihongyu.com/

你可能感兴趣的文章
操作系统简述
查看>>
设计模式大总结2-结构型模式
查看>>
【Python】不定期更新学习小问题整理
查看>>
Zico源代码分析:执行启动过程分析和总结
查看>>
Android之Http通信——1.初识Http协议
查看>>
hdu5044(二分)
查看>>
静态路由、缺省路由的作用
查看>>
linux快捷键绝对路径相对路径讲解
查看>>
又漏一次
查看>>
dede模板中plus文件路径使用方法
查看>>
xml解析demo使用
查看>>
python使用模板手记
查看>>
No result defined for action com.nynt.action.ManageAction and result input问题
查看>>
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
查看>>
洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
查看>>
c++ 连接数据库
查看>>
函数指针与回调函数
查看>>
你所不知道的 CSS 滤镜技巧与细节
查看>>
hack reviewboard 让 FireFox 把 tab 显示为 4 或 2 个空格
查看>>
css 学习整理
查看>>