Main Content
I recently encountered an algorithm problem in the Chuanzhi forum. I think there is an interesting solution. The following calculation method may not be optimal, but you can refer to it.
Problem:
========================================================== ====================================
Given two strings, consisting of only lowercase letters, they contain the same character. Change the first string
The minimum number of operations for the second string, and each operation can only move a character from the first string
The start of the string.
For example, if two strings "abcd" "bcad" are specified, the output is: 2, because you need to perform the operation twice to change "abcd"
To "bcad", the method is: abcd-> cabd-> bcad.
I started to use breadth-first search. For example, abcdefg and gfedcba are recursive to the 7th Power.
Time complexity, and then report the number of cycles in the function of changing the position.
Then I used the List to record the items that appeared in abcdefg and removed them, that is, 7*6*5*4*3*2.
However, every time you judge that list. contain () is inside, it seems that the cycle time is the same as above.
Is there a better solution.
========================================================== ======================================
This problem is interesting. Here I use C # To solve it. I mainly consider that Microsoft provides many data structures and does not need to re-implement the data structure and related methods.
Problem Analysis
Then we can regard the original string as a data structure waiting for sorting to simplify the problem into simple sorting calculation.
Method: Specify the order.
For example, if you change the string "abcd" to "bcad", then the character set "bcad" forms an ordered set and the ing table is displayed as follows:
{('B', 0), ('C', 1), ('A', 2), ('D', 3 )} therefore, the string to be sorted becomes a number set: {2, 0, 1, 3 }. that is, you need to sort the dataset to {0, 1, 2, 3}. The minimum step is required. because
This translates the problem into a sorting least step problem.
On the other hand, the problem takes into account the requirements of algorithm implementation, that is, each time only one data can be taken out and inserted to the starting place. That is to say, the specific operation is a fixed function:
Insert item I to item 0, and the data of the original I-1 item is moved one by one. So the method is simple:
jk_remove([] s, temp = ( i = index - ; i >= ; i-- s[i + ] = s[] = }
The number of calls to this function indicates the number of operations performed.
Another problem is considered below. when is this function called. that is, how to operate is superior. this problem is troublesome. at first, I planned to find one from a ready-made algorithm. but they are not very friendly. think
The "reverse" method is used.
The so-called minimum step is not to move the redundant step. As mentioned above, the problem has been categorized as a sorting problem. Please use the ascending order below.
To sort a numerical sequence, you can only move the data forward. The smallest step is to move a data sequence each time and the data is a good sequence. That is to say, the number will not move again. then a length is
N string, up to n-1 times. I think this should be the least step !!!
Next, let's see how to move it.
The first feature of moving is that every time data is moved, it is placed at the beginning. at the same time, after the next movement starts, the data that has been moved will be automatically pushed to the back. so we can stop moving this data repeatedly.
First, move the largest number. Then, after moving the remaining largest number, the result will be good.
Sorted.
So what data needs to be moved? Very simple reverse order (Note 1). For example:
Continue the previous example, number 2013. the first number does not need to be considered. The second number 0 and 2 constitute a reverse order, so 0 is the reverse order. numbers 1 and 2 constitute a descending order, that is, 1 is a descending order. first, move a large volume,
Move 1 to 1203. As the data arrangement changes, you need to re-calculate the reverse order. Therefore, 0 is the reverse order.
If the value is 0, 0123 is displayed.
Here we can draw a conclusion that we can determine the reverse order and move the largest number of reverse orders. We can get the pseudo code:
jk_sort( temp = ; ( i = ; i < s.length; i++ ( j = ; j < i; j++ (s[i] < (temp == || temp < temp = (temp != }
Recursion is used here, and the complexity of the algorithm is improved. The specific optimization can be discussed later.
After the path, you can implement it.
Code Implementation
First write a class
}
Provides static methods to move data
jk_remove([] s, temp = ( i = index - ; i >= ; i-- s[i + ] = s[] = }
Provides ing. You can consider using a key-value pair.
Dictionary<, > dic; JKSort( dic = Dictionary<, > ( i = ; i < chs.Length; i++ }
internal_jk_sort([] s, Dictionary<, > temp = -; ( i = ; i < s.Length; i++ ( j = ; j < i; j++ (dic[s[i]] < (temp == - || dic[s[temp]] < temp = (temp != - }
How to make public
JK_Sort( str, [] chs = [] obj = JKSort j = }
Then the sorting is completed. The algorithm provided here is not optimized. For example, you want to know the optimization and the algorithm with repeated characters.
String solution, please wait for the following.
Go to bed at AM, January 1, December 18, 2013.
Note 1
Reverse Order: In a number arrangement, if one of the two numbers is larger than the other, it is called
They constitute an inverse order. Conclusions and problems related to the inverse order can be referred to "Higher Algebra" or "Linear Algebra".
.