The Application of binary notation in composite mathematics

Source: Internet
Author: User

There is a string array r = [ABC, Bac, ACB]
I only want to retain one of the strings, that is, to delete strings with the same content but different character combinations.

 

 

I will introduce you to the single-digit operation technique, which is limited to less than 32 letters.

Assume that the ASC code of the letter A is 97, then we subtract 97, so that the letter A-Z will be compiled from 0 to 25.
A = 0; B = 1; C = 2; D = 3; E = 4; F = 5; G = 6 ....
To determine whether there is a in a string, we can observe the following mathematical formula:
Alert (1 <0); // The result is 1.
Alert (1 <1); // The result is 2
Alert (1 <2); // The result is 2
Alert (1 <3); // The result is 8.
Alert (1 <4); // The result is 16.
<Indicates the Left shift operation, which is equivalent to multiplication 2. The number of shifts is the power of 2.

64 stands for 6, which is the power of 6. If the number of a string is calculated as 64, the string can only contain 6, that is, only one letter G.

65 split into a multiple of 2 and the French, is 65 = 64 + 1 = (1 <6) + (1 <0), representing 6 and 0 inside.
That is, letters A and F, A is 0, and F is 6.
65 = (1 <6) + (1 <0)
This string is either Ag or GA

Observe the three identical letter combinations ABC, Bac, and ACB.
ABC = (1 <0) + (1 <1) + (1 <2) = 7;
BAC = (1 <1) + (1 <0) + (1 <2) = 7;
ACB = (1 <0) + (1 <2) + (1 <1) = 7;

7 indicates the combination of the string ABC, which has only a few letters.

Do you understand the preceding method of converting strings into numbers?

This technique is also widely used within the operating system, such as file attributes.
Normal 0 common file. Do not set attributes.
Readonly 1 read-only file. The attribute is read/write.
Hidden 2 hides the file. The attribute is read/write.
System 4 system file. The attribute is read/write.
Volume 8 disk drive volume label. The attribute is read-only.
Directory 16 folder or directory. The attribute is read-only.
The archive 32 file has been modified since the last backup. The attribute is read/write.
Alias 64 link or shortcut. The attribute is read-only.
Compressed 128. The attribute is read-only.

If the read-only mode is 1 and the hidden mode is 2, both the read-only mode and the hidden mode are system files. 7 means 1 + 2 + 4 = (1 <1) + (1 <2) + (1 <3)
It is actually very easy to judge whether 7 contains 2, as long as a "and" operation is performed:
Alert (7 & (1 <2 ))! = 0); // true
Similarly, it is very easy to determine whether 65 contains 2, 0, and 6, as long as the "and" operation is performed:

Alert (65 & (1 <2 ))! = 0); // false
Alert (65 & (1 <0 ))! = 0); // true
Alert (65 & (1 <6 ))! = 0); // true

Based on the above techniques, all letters can be compiled into numbers, and all strings can be compiled into numbers.
Therefore, the repeated judgment of string combinations becomes the repeated judgment of integers.

Whether integers are repeated or not, you can use Hash Table complexity O (N), binary number complexity is O (n * log n)

Here we provide a javascript hash table writing method, and the rest of the code can be done by themselves.
<Script language = "JavaScript">
// Custom hash table class
Function hashtable ()
{
This. _ Hash = new object (); // create an object
// How to add a hash table
This. Add = function (Key, value ){
If (typeof (key )! = "Undefined "){
If (this. Contains (key) = false ){
This. _ Hash [Key] = typeof (value) = "undefined "? Null: value;
Return true;
} Else {
Return false;
}
} Else {
Return false;
}
}
// How to remove a hash table
This. Remove = function (key) {delete this. _ Hash [Key];}
// Number of keys in the hash table
This. Count = function () {var I = 0; For (var k in this. _ Hash) {I ++;} return I ;}
// Obtain the hash table value through the key value
This. Items = function (key) {return this. _ Hash [Key];}
// Judge whether a value exists in the hash table
This. Contains = function (key) {return typeof (this. _ Hash [Key])! = "Undefined ";}
// Method for clearing the content of the hash table
This. Clear = function () {for (var k in this. _ Hash) {delete this. _ Hash [k] ;}}

}
VaR myhash = new hashtable (); // create a hash table
Myhash. Add ("name", "Zhang San"); // Add key and Value
Alert (myhash. item ["name"]); // display the hash table value based on the specified key
</SCRIPT>

It is of little significance to develop algorithms in Javascript. However, if the same question is in a strong language, the above idea is definitely pursuing the fastest speed.
It is theoretically less complex.

Also, through some of my ideas above, I will deepen my understanding of the application of the binary system in composite mathematics and the basic data structure.

The preceding algorithm assumes that the total length of the input string is N.
The encoding complexity is O (n)
The complexity of inserting hash tables without duplicates is P (n)
The total complexity is still in linear O (n) + P (n)

Theoretically, it is the fastest. If you do not believe it, you can compare the total computing time of tens of thousands of strings.
I should have produced results in an instant, and the algorithms of those above will cause no response.
Because their algorithm complexity is the geometric base, the input number increases, and the calculation amount increases exponentially.

I will not write the specific code, but leave it to those who are interested. In the future, I may become the godfather of a generation.

Wukong, go to tianyao with me .....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.