# [Data structure] Some interesting topics (i)

Source: Internet
Author: User

1, ask for two number of greatest common divisor

[CPP]View Plaincopy
1. int gcd (int i,int j)
2. {
3. While (i! = j)
4. {
5. if (i > J)
6. {
7. I-= j;
8. }
9. Else
10. {
11. J-= i;
12. }
13. }
14. }

2, move the ABCDEFG string to the left three bits into DEFGABC
Method:
Step three: cbagfed---> DEFGABC
The advantage of this method is that it requires less storage space,

3, C language computing Hanoi min Moving Steps (i)
When only one plate is n=1, the minimum number of steps is S (1) =1
When n=2 with two plates, the minimum number of steps is S (2) =s (1) *2+1
When n=3 with three plates, the minimum number of steps is S (3) =s (2) *2+1

And so on, can use recursive method s (n) =s (n-1) *2+1;

4, asks if the A string contains the letters in the B string

Suppose that there is a string a of various letters, and another string B, the number of letters B in the string is relatively small. What is the quickest way to find out all the letters in the small string B are in the large string a? For example, if it is the following two strings:
String 1:ABCDEFGHLMNOPQRS
String 2:dcgsrqpo
The answer is true, and all the letters in string2 are string1.

Method 1, the two strings are sorted, and then compared, the time complexity is O (MLOGM) +o (NLOGN) +o (m+n)
Method 2, the method of counting sorting, the time complexity is O (n+m) +o (n+m) =o (n+m). Determine if space is occupied
Method 3,hash Table method, time complexity is O (n+m)
A, hash[26], first clear all zeros, and then scan the short string, if there is a corresponding 1,
b, calculate the number of hash[26] 1, recorded as M
C, scan the long string of each character A; if the original hash[a] = = 1, then modify hash[a] = 0, and will m minus 1, if hash[a] = = 0, then do not do the processing
D, if m = = 0 or scan end, exit loop.

[CPP]View Plaincopy
1. #include <iostream>
2. #include <string>
3. Using namespace std;
4. int main ()
5. {
6. String str1="ABCDEFGHLMNOPQRS";
7. String str2="Dcgsrqpom";
8. //Open an auxiliary array and clear 0
9. int hash[26] = {0};
10. //num is the number of elements in the secondary array
11. int num = 0;
12. //Scan short string
13. For (int j = 0; J < Str2.length (); j + +)
14. {
15. //Convert the character to an index in the corresponding auxiliary array
16. int index = str1[j]- ' A ';
17. //If the corresponding element of the index in the auxiliary array is 0, then 1, and num++;
18. if (hash[index] = = 0)
19. {
20. Hash[index] = 1;
21. num++;
22. }
23. }
24. //Scan long string
25. For (int k = 0; k < str1.length (); k++)
26. {
27. int index = str1[k]- ' A ';
28. //If the index corresponding element in the secondary array is 1, then num--, or zero, no processing (no write statement).
29. if (Hash[index] ==1)
30. {
31. Hash[index] = 0;
32. num--;
33. if (num = = 0) //m==0, that is, exit the loop.
34. Break ;
35. }
36. }
37. //num is 0 description long string contains all characters in short string
38. if (num = = 0)
39. cout << "true" << Endl;
40. Else
41. cout << "false" << Endl;
42. return 0;
43. }

Method 4, Prime method
A. Define a minimum of 26 primes corresponding to the character ' a ' to ' Z ', respectively.
B. Traverse a long string to obtain the product of the corresponding prime number for each character.
C. Traverse the short string to determine whether the product is divisible by the prime number of characters in the short string.
D. Output results.

[Data structure] Some interesting topics (i)

Related Keywords:

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.