1, ask for two number of greatest common divisor
[CPP]View Plaincopy
- int gcd (int i,int j)
- {
- While (i! = j)
- {
- if (i > J)
- {
- I-= j;
- }
- Else
- {
- J-= i;
- }
- }
- }
2, move the ABCDEFG string to the left three bits into DEFGABC
Method:
First step: ABCDEFG---> CBADEFG
Step two: CBADEFG---> cbagfed
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
- #include <iostream>
- #include <string>
- Using namespace std;
- int main ()
- {
- String str1="ABCDEFGHLMNOPQRS";
- String str2="Dcgsrqpom";
- //Open an auxiliary array and clear 0
- int hash[26] = {0};
- //num is the number of elements in the secondary array
- int num = 0;
- //Scan short string
- For (int j = 0; J < Str2.length (); j + +)
- {
- //Convert the character to an index in the corresponding auxiliary array
- int index = str1[j]- ' A ';
- //If the corresponding element of the index in the auxiliary array is 0, then 1, and num++;
- if (hash[index] = = 0)
- {
- Hash[index] = 1;
- num++;
- }
- }
- //Scan long string
- For (int k = 0; k < str1.length (); k++)
- {
- int index = str1[k]- ' A ';
- //If the index corresponding element in the secondary array is 1, then num--, or zero, no processing (no write statement).
- if (Hash[index] ==1)
- {
- Hash[index] = 0;
- num--;
- if (num = = 0) //m==0, that is, exit the loop.
- Break ;
- }
- }
- //num is 0 description long string contains all characters in short string
- if (num = = 0)
- cout << "true" << Endl;
- Else
- cout << "false" << Endl;
- return 0;
- }
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)