Chapter 4 string
I. multiple choice questions
1. Which of the following statements about strings is incorrect? () [North Jiaotong University 2001 I, 5 (2 points )]
A. A string is a finite sequence of characters B. An empty string is a string consisting of spaces
C. pattern matching is an important operation of strings. D. Strings can be stored sequentially or in a chain.
2. If S1 = 'abcdefg', S2 = '123', S3 = '###', S4 = '123', run
Concat (replace (S1, substr (S1, length (S2), length (S3), S3), substr (S4, index (S2, '8 '), length (S2 )))
The result is () (1999-1, 5 (25/7 points )]
A. ABC ### 2017123 B. ABCD ### 2345 C. ABC ### g2345 D. ABC ### 2345
E. ABC ### g1234 F. ABCD ### 1234G. ABC ### 01234
3. There are two strings p and q. Q is a substring of P, and the algorithm for finding the position where Q first appears in P is called ()
A. substring B. Join C. Match D. Evaluate String Length
[Beijing University of Posts and Telecommunications 2000 II and 4 (20/8 points)] [Xi'an University of Electronic Science and Technology 1996 I and 1 (2 points )]
4. The next array value of the known string S = 'aaab' is (). Xi'an University of electronic science and technology (1996 )]
A.0123 B .1123 c.1231 d.1211
5. The next array of the string 'ababaaababa' is (). Sun Yat-sen University 1999 I and 7]
A.012345678999 B .0121111612 c.0000034223456 d.0123012322345
6. The nextval of the string 'ababababab' is ()
A. (,) B. (, 1)
C. (, 1) D. (, 1)
[Beijing University of Posts and Telecommunications 1999 I, 1 (2 points )]
7. The mode string T = 'abcaabbcabcaabdab'. The value of the next array of the mode string is (), and the value of the nextval array is ().
A.0 1 1 1 2 1 1 1 2 3 4 5 6 7 1 2 B .0 1 1 1 2 2 1 2 3 4 5 6 1 1 2 2
C.0 1 1 0 0 1 3 1 0 1 1 0 0 7 0 1 d.0 1 1 1 2 2 3 1 2 3 4 5 6 7 1 2 2
E.0 1 1 0 0 1 1 1 0 1 0 1 0 1 7 0 1 f.0 1 1 0 2 1 1 1 0 1 0 2 1 7 0 1 1 1 1 1 1 1 1
(Beijing University of Posts and Telecommunications 1998, (2 points )]
8. If the string S = 'soft', the number of its substrings is (). Xi'an University of Electronic Science and Technology 2001 application 1 and 2 (2 points )]
A.8 B .37 c.36 D.9
9. set S to a string with the length of n. If the characters are different, the substring in S is an extraordinary substring (non-null and different from S) the number is (). Institute of computing, Chinese Emy of Sciences 1997]
A.2n-1 B. n2 C. (n2/2) + (n/2) d. (n2/2) + (n/2)-1 E. (n2/2)-(n/2)-1 F. other cases
10. The length of the string refers to () [Beijing University for Industry and Commerce 2001 I, 6 (3 points )]
A. Number of different letters contained in the string B. number of characters contained in the string
C. Number of different characters contained in the string D. Number of non-space characters contained in the string
Ii. Answer questions
1. The KMP algorithm indicates that the pointer of the primary string will not decrease during pattern matching. () [Beijing University of Posts and Telecommunications 2002 I and 4 (1 point )]
2. set the pattern string length to m and the target String Length to n. When n ≈ m and the processing only matches once, the simple match (I .e., the substring locating function) the algorithm may save more time. () [Changsha railway College, 1998 I, 1 (1 point )]
3. A string is a linear table with special data objects and operations. () [Dalian Maritime University 2001 1, L (1 point )]
2. Fill in blank questions
1. Space string refers_ (1 )__, Whose length is equal___ (2 )__. Xi'an University of Electronic Science and Technology 2001 software 1 and 4 (2 points )]
2. The data element of the string can only be ________. Sun Yat-sen University 1998 I and 5 (1 point )]
3. Which of the following is the substring of a string. Huazhong University of Science and Technology (2000 )]
4. Index ('datastructure ', 'str') = ________. Fuzhou University 1998 (2 points )]
5. If the length of the text string is n and the length of the mode string is m, the time complexity of the KMP algorithm for string matching is ________.
[Chongqing University, 2000]
6. The next function value sequence of the pattern string P = 'abababc' is ________. Xi'an University of Electronic Science and Technology 2001 software 1 and 6 (2 points )]
7. The nextval function value of the string 'ababaaab' is ________. [Beijing University of Posts and Telecommunications 2001, (2 points )]
8. Set T and P to two given strings. The process of finding a substring equal to P in T is called_ (1 )__, Also known as P_ (2 )__.
Xi'an University of electronic science and technology (1998, 16/6 )]
9. A string is a special linear table. Its particularity is manifested in_ (1 )__The two most basic storage methods of strings are:_ (2 )__,_ (3 )__; The necessary and sufficient condition for the two strings to be equal is_ (4 )__. [China University of Mining and technology, 2000 I, 3 (4 points )]
10. The necessary and sufficient conditions for the two strings to be equal are _______. Xi'an University of Electronic Science and Technology Software 1 and 1 (2 points )]
11. zhiu = 'xyxyxyxxyxy'; t = 'xxy ';
Assign (S, U );
Assign (v, substr (S, index (S, T), Len (t) + 1 ));
Assign (M, 'ww ')
Replace (S, V, m) = ________. [Northeastern University 1997 I, 1 (5 points )]
12. The strcpy function for copying strings is:
Void strcpy (char * s, char * T)/* Copy t to S */
{While (________)
} [Zhejiang University 1999 I, 5 (3 points )]
13. The following program checks whether the string S is symmetric. If the string S is symmetric, 1 is returned. Otherwise, 0 is returned. If F ("Abba") returns 1, F ("Abab") returns 0;
Int F ((1 )________)
{Int I = 0, j = 0;
While (s [J])(2 )________;
For (j --; I <J & S [I] = s [J]; I ++, j --);
Return ((3 )_______)
} [Zhejiang University, 1999 I, 6 (3 points )]
14. The following algorithms are used to obtain the longest common substring of string S and string t stored in the sequential structure.
Program ()
Procedure maxcomstr (var s, T: orderstring; var index, Length: integer );
VaR I, J, K, length1: integer; con: Boolean;
Begin
Index: = 0; Length: = 0; I: = 1;
While (I <= S. Len) Do
[J: = 1;
While (j <= T. Len) Do
[If (s [I] = T [J]) then
[K: = 1; length1: = 1; con: = true;
While con do
If(1 )__Then [length1: = length1 + 1; K: = k + 1;] else(2 )_;
If (length1> length) then [index: = I; Length: = length1;]
(3 )____;
]
Else(4 )____;
]
(5 )___;
]
End;
Procedure (B)
Void maxcomstr (orderstring * s, * t; int index, length)
{Int I, J, K, length1, con;
Index = 0; length = 0; I = 1;
While (I <= S. Len)
{J = 1;
While (j <= T. Len)
{If (s [I] = T [J])
{K = 1; length1 = 1; con = 1;
While (CON)
If(1 )_{Length1 = length1 + 1; k = k + 1;} else(2 )__;
If (length1> length) {Index = I; length = length1 ;}
(3 )____;
}
Else(4 )___;
}
(5 )__
} [Shanghai University 1 and 2 (10 points )]
15. Improved Algorithm: Find the next array in the KMP algorithm.
Proc get _ next (T: String, VAR next: array [1 .. t. Len] of integer );
Begin
J: = 1; K: =(1 )__; Next [1]: = 0;
While j <t. Len do
If K = 0 or T. ch [J] = T. ch [k] Then begin J: = J + 1; K: = k + 1; next [J]: = K; End
Else K: =(2 )___;
End;
Sun Yat-sen University 1998 IV. 1 (4 points )]
16. The following function index is used to determine whether T is a substring of S. If the return value is the sequence number of the first occurrence of T in S (starting from 1), otherwise 0 is returned.
For example, if S = 'abcdefcdek' and t = 'cde', indse (s, t) = 3, index (S, 'aaa') = 0. Known t, s string length are Mt, MS
Func index (S, T, MS, MT );
I: = 1; J: = 1;
While (I <ms) and (j <MT) Do
If s [I] = T [J] Then [(1 )__;(2 )__]
Else [(3 )___;(4 )_]
If j> Mt then return(5 )____; Else return(6 )__
ENDF;
[Nanjing University of Technology, 1999, (6 points )]
17. Read the following program description and PASCAL program and write the words that should be filled in () on the answer sheet.
Program description:
This program is used to determine whether the input string is a string in the following format:
W & M $ where, the sub-string m is the character reverse arrangement of the sub-string W. It is assumed that w does not contain characters & and characters $, and the character & is used as the separator between w and M, character $ is used as the string input Terminator.
For example, for input strings AB & Ba $, 11 & 12 $, AB & DD $, and & $, the program will output OK. (YES), No. (No) respectively ).
Program
Program accept (input, output );
Const midch = '&'; endch = '$ ';
VaR an: Boolean; Ch: Char;
Procedure match (VAR answer: Boolean );
VaR values, CH2: Char; F: Boolean;
Begin
Read (bytes );
If latency <> endch
Then if(1 )__
Then begin match (f );
If F then begin read (CH2); answer: =(2 )_End else answer: = false
End
Else(3 )___
Else(4 )___
End;
Begin
Writeln ('enter string :');
Match ();
If an then begin
(5 )__If(6 )_Then writeln ('OK.') else writeln ('no .')
End
Else writeln ('no .')
End. [Shanghai Marine college 1998 (15 points )]
18. Use the following stack and string operations to complete the following blank questions.
Initstack (s) sets s as an empty stack;
Push (S, x) elements X into the stack;
Pop (s) Out-of-stack operations;
Gettop (s) returns the top element of the stack;
Sempty (s) judge the empty stack function;
Setnull (ST) sets the string st to an empty string;
Length (ST) returns the length of the string st;
Equal (S1, S2) judges whether the S1 and S2 functions are equal;
Concat (S1, S2) returns the string after S1 and S2 are connected;
Sub (S, I, 1) returns the I-th character in S;
Empty (ST) Judge string null Function
Func invert (pre: string; var Exp: string): Boolean;
{If the prefix pre of the given expression is correct, the corresponding expression exp is obtained in this process and "true" is returned. Otherwise, exp is an empty string and "false" is returned ". It is known that the original expression does not contain an arc, and opset is a set of operators .}
VaR S: Stack; I, n: integer; succ: Boolean; Ch: Char;
Begin
I: = 1; N: = length (pre); succ: = true;
(1 )__;(2 )__;
While (I <n) and succ do
Begin CH: = sub (PRE, I, L );
If(3 )_Then(4 )__
Else if(5 )__Then(6 )_
Else begin
Exp: = Concat ((7 )___,(8 )____);
Exp: = Concat ((9 )___,(10 )___);
(11 )__;
End;
I: = I + 1
End;
If(12 )___Then
Begin Exp: = Concat (exp, sub (PRE, N, 1); invert: = true end
Else begin setnull (exp); invert: = false End
End;
Note: Only one statement is required for each space. Tsinghua University 1996]
Iv. Application Questions
1. Glossary: [Dalian Maritime Affairs 1996 I and 10 (1 point)] [Hehai University 1998 II and 5 (3 points )]
2. Differences between the following concepts: Space string and empty string. Dalian Maritime University 1996 III. 2. (1) (2 points )]
3. the lengths of the two strings S1 and S2 are M and N, respectively. The time complexity of the algorithm for finding the maximum common substrings of the two strings is T (m, n ). Estimate the optimal T (m, n) and briefly explain the reasons. [Beijing University of Technology, 1996 I, 5 (6 points )]
4. Set the main string S = 'xxyxxxyxxxxyxyx', and the mode string T = 'xxyxy '. Excuse me: How can I find the location where T appears in s with the least number of comparisons? What is the number of comparisons? Dalian Maritime University 2001 IV (8 points )]
5. What are the improvements of the KMP algorithm (String Matching Algorithm) compared with the brute (simple string matching) algorithm? Dalian Maritime University 1996 III. 1 (2 points )]
6. The known pattern string T = 'ababbabcab' writes the next and nextval function values corresponding to each character obtained by the KMP method.
[Beijing University of Posts and Telecommunications 1997 (10 points )]
7. The next and nextval arrays of the 'abacabaaad' strings in the KMP algorithm are provided. [Beijing University of Posts and Telecommunications 2000 )]
8. Make T = 'abcaba' and obtain the value of the next function and the value of the nextval function. 1994 (6 points) of North Jiaotong University )]
9. The known string 'cddcdececdea 'is used to calculate the values of the next and nextval functions for each character. [Nanjing University of Posts and Telecommunications 2000-2]
10. Use the KMP algorithm and the improved algorithm to calculate the next and nextval functions of P1 = 'ababaaa' and P2 = 'aabab' respectively.
Southeast University 1999 )]
11. It is known that the substring of the kmp string matching algorithm is babababaa. Write the next array information value after the next array is improved (the starting point of the array subscript must be written ). (Southwest Jiaotong University, 2000]
12. Evaluate the value of next (j), the failure function of the pattern string T = 'abababbac. Xi'an Jiaotong University 1996 IV and 4 (5 points )]
13. How is the failure function defined in the string pattern matching KMP algorithm? Calculation mode string P = 'aabaabaaabc' failure function value of each character. [University of Petroleum 1998 1, 2 (10 points )]
14. Set string S = 'aabaabaabaac ', P = 'aabaac'
(1) give the next value and nextval value of S and P;
(2) If S is the primary string and P is the pattern string, the matching process using BF algorithm and KMP algorithm is given.
(Beibei Jiao Tong University, December 1998 )]
15. Set the target to T = 'abcaabbabcabaacbacba ', and the mode is P = 'abcaba'
(1) Calculate the naxtval function value of model P (5 points)
(2) If you do not write an algorithm, you can only draw the matching process for each line of pattern matching using the KMP algorithm. (5 points)
Tsinghua University 1998 (10 points )]
16. the pattern matching algorithm is an effective method to quickly find the pattern in the master string. If the length of the master string is set to m, the pattern length is n, what is the time complexity of the mode-based KMP algorithm in the main string? If a mode is P = 'abcaacabaca ', provide its next function value and the value of the next function's corrected value nextval. Shanghai Jiao Tong University 2000 (5 points )]
17. Set the target to S = 'abcaabbcaaababaabca ', and the mode is P = 'babab ',
(1) The value of the nextval array in the manual calculation mode P; (5 points)
(2) write out the process of matching the target s by the KMP algorithm using the obtained nextval array. (5 points)
Tsinghua University 1997 (10 points )]
18. Use the non-backtracking pattern matching method (KMP method) and the fast non-backtracking pattern matching method to find the next [J] value of the pattern string T, and add it to the following table:
J |
1 2 3 4 5 6 7 |
T |
A A B |
Next [J] value obtained by the KMP Method |
|
The next [J] value obtained by the rapid non-Backtracking Method |
|
[Beijing University of Posts and Telecommunications 1992, 25/4 (points )]
19. In the improved (no backtracking) string pattern match, evaluate the value of the next array first. The following is an algorithm used to evaluate the nextval value.
Type SAR = array [1 .. m] of integer;
Pty = array [1 .. m] of char;
Procedure next2 (P: Pty; var nextval: SAR );
{Evaluate the value of the nextval array in mode P}
1 begin
2 J: = 1; nextval [1]: = 0; K: = 0
3 repeat
4 If (k = 0) or (P [J] = P [k])
5 then [J: = J + 1; K: = k + 1;
6 if p [J] = P [k]
7 then nextval [J]: = nextval [k]
8 else nextval [J]: = K]
9 else K: = nextval [k]
10 until J = m
11 end;
In the algorithm, the first row contains P [J] = P [K], and the sixth row also contains P [J] = P [K]. The two comparison statements are the same. What are the meanings of these two comparison statements? Analyze the time complexity of this algorithm in the worst case? (Beijing University of Posts and Telecommunications 1993, 2 (6 points )]
20. In the string mode matching KMP algorithm, the next array value of the pattern is defined as follows:
Next [J] =
Excuse me:
(1) When j = 1, why should we use next [1] = 0?
(2) Why max {k?
(3) What are other situations? Why do we use next [J] = 1? Beijing University of Posts and Telecommunications 1994 (8 points )]
21. Define the failure function F in the KMP algorithm and describe the rules for matching string patterns using F. What are the technical features of this algorithm?
[Southeast University 1993 I, 3 (9 points) 1997 I, 2 (8 points) 2001 I, 6 (6 points )]
22. In the definition of the failure function f used in the mod-Test Matching KMP algorithm, why do we need P1P2 ...... PF (j) Is P1P2 ...... What are the matched substrings of PJ? And is the largest real substring? [Southeast University 1996 I, 3 (7 points )]
23. Can two strings contain equal characters? Xi'an University of Electronic Science and Technology 2000 software 1 and 3 (5 points )]
24. Set S1 and S2 as strings. provide all possible conditions for making S1 // S2 = S2 // S1 (// a connector ).
[Changsha railway college 1997 (3 points)] [National Defense University of Science and Technology 1999 I]
25. Known: S = '(XYZ) + *', t = '(x + Z) * Y '. Use basic operations such as join, substring and replacement to convert s to T.
[North Jiaotong University 1996 I, 3 (5 points)] [Shandong University of Science and Technology 2002 I, 6 (5 points )]
Part 5: Algorithm Design
1. Set S and T as two strings, which are placed in two one-dimensional arrays respectively. m and n are their respective lengths to determine whether T is a substring of S. If yes, the position of the output substring (the first character); otherwise, the output is 0. (Note: Implemented by program) [Nanjing University of Aeronautics and Astronautics 1997 (10 points )]
2. Enter a string containing numbers and non-numeric characters, such as ak123x456 17960? 302gef4563: store consecutive numbers in an array A as a whole, for example, 123 in a [0], 456 in a [1],… ... . Calculate the total number of integers and output these integers. Shanghai University 1998 (13 points )]
3. the string is represented by a sequential storage structure and an algorithm is designed. Calculate the first longest repeated substring in string s and its location, and analyze the time complexity of the algorithm. Southeast University 2000 (15 points )]
Other statements similar to this question include:
(1) If each character of a substring of a string (its length is greater than 1) is the same, it is called an equivalent substring. Design an algorithm and input the string s to "!" As the end sign. If no equi-substring exists in string S, the output information is "No equi-le-substring". Otherwise, an equi-string with the maximum length is obtained (output.
For example, if S = "abc123abc123 !", The output is "non-equivalent substrings". If S = "abceebccadddddaaadd !", Output "ddddd ".
Huazhong University of Science and Technology, 2001]
4. If the storage structure of the string is as follows, write an algorithm to replace the string. Tsinghua University 5-1995 (15 points )]
Type strtp = record
Ch: array [1 .. maxlen] of char;
Curlen: 0 .. maxlen
End;
5. The Void insert (char * s, char * t, int POS) function inserts string t into string S, and the insertion position is POS. Use C language to implement this function. Assume that the space allocated to string S is sufficient for string t insertion. (Note: Do not use any library function)
Beijing University of Aeronautics and Astronautics 2001 (10 points )]
6. Design a binary search algorithm to find the given string in a group of strings. Assume that the length of all strings is 4.
(1) briefly describe the main idea of the algorithm; (3 points)
(2) Use Pascal to describe the types and variables used in the algorithm respectively. (3 points)
(3) Use Pascal-like language or natural language to write non-recursive processes of algorithms; (8 points)
(4) analyze the maximum search length of the algorithm; (3 points)
(5) add Chinese comments as necessary. (3 points)
Shandong Industrial University 1995 (20 points )]
7. Design a Pascal or C language function atoi (x). X is a string consisting of ten characters ranging from 0 to 9 and '-' representing a positive or negative number. The return value is an integer value. Zhejiang University 1994 (7 points )]
8. it is known that the string S1 contains an English segment. The algorithm format (S1, S2, S3, n) is written, and the string S2 is formatted into an alignment string at both ends according to the given length N, the extra characters are sent to S3. Capital University of Economics and Trade 1998 )]
9. strings are stored in a static storage structure. The structure is described below. We try to implement the equal Algorithm for string operations.
Const maxlen = Maximum length of the string to be confirmed
Type strtp = record
Ch: array [1 .. maxlen] of char;
Curlen: 0 .. maxlen
End;
(Store string values in a one-dimensional array, and set the indicator curlen to indicate the current String Length) [Beijing Light Industry University 1998 (12 points )]
10. Write a program to count the frequency of occurrence of different characters in the input string and save the result to the file (the valid character in the string is the 26 letters of the A-Z and the 10 digits of the 0-9 ). Northwestern University 2000 (10 points )]
11. Write a recursive algorithm to store strings in reverse order. No additional string storage space is required. Southwest Jiaotong University 2000 III and 2]
12. three strings are known to be s = 'AB... Abcaabcbca... A', s' = 'caab', s' = 'bcb '. The result string obtained by using the function of the basic string operation is: s ''= 'caabcbca... ACA... A', you must write out the functions and execution algorithms used to obtain the result string s. [Northeastern University 1998 I, 1 (10 points )]
13. S = "s1s2... Sn is a string of N length, which is stored in an array. After the encoding program modifies S, It outputs:
(1) Place all the even numbers of characters in S in the descending order of their original subscript in the second half of S;
(2) Place all the odd characters of S in the First Half of S in the ascending order of their original subscript;
For example:
S = 'abcdefghijkl'
Then the transformed S is 'acegikljhfdb '. Institute of computing, Chinese Emy of Sciences 1995]
14. Compile a program and output the token of the input expression (string. The expression is a, B, c, constant (number) 0, 1 ,..., 9. The operator +, *, and brackets. First, define the class code of the symbol:
Symbol |
Variable |
Constant |
* |
+ |
( |
) |
Class Code |
0 |
1 |
2 |
3 |
4 |
5 |
Next, define the token representation of the symbol:
Namel is a variable name table (the same name is not allowed), and const is a constant table (the same number is not allowed ).
For example, if there is an expression (A + A * 2) + 2 * B * 3 #, the following tokenl will be generated:
[Jilin University, 1995 (20 points )]