String processing methods, apparatus, devices and media
By using a trie structure and the maximum prefix matching method in string processing, the problem of low efficiency in shortest string concatenation in existing technologies is solved, and efficient string concatenation is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- AGRICULTURAL BANK OF CHINA
- Filing Date
- 2022-09-30
- Publication Date
- 2026-07-31
AI Technical Summary
Existing technologies suffer from high time complexity and low efficiency when performing the shortest string concatenation, and cannot efficiently merge multiple strings into the shortest concatenated string.
We use a trie structure to store the substring set and perform string concatenation using the maximum prefix matching method. By leveraging the space-for-time tradeoff of the trie, we reduce the time complexity of maximum prefix matching and improve concatenation efficiency.
By using a trie structure to achieve maximum prefix matching, the time complexity of string concatenation is reduced, and the efficiency and accuracy of concatenation are improved.
Smart Images

Figure CN115563353B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, and in particular to a string processing method, apparatus, device and medium. Background Technology
[0002] A string is a sequence of characters consisting of one or more of the following classes of characters: numbers, letters, and underscores. It is one of the most commonly used data structures. In software development, there are various ways to manipulate strings: inserting or deleting a substring or character, searching for and locating a specific substring within a string, and / or performing maximum prefix matching in a browser's keyword suggestion function, etc.
[0003] String concatenation is also a common string manipulation technique. One approach involves simple concatenation, rather than merging two or more strings into the shortest possible string. For example, merging "ABCD" and "CDEF" into "ABCDEF" instead of "ABCDCDEF". Another approach uses brute-force matching or the KMP (Knuth-Morris-Pratt) matching method to merge two or more strings into the shortest possible string; however, this method has high time complexity and low efficiency.
[0004] Therefore, the high time complexity and low efficiency of string concatenation are problems that urgently need to be solved. Summary of the Invention
[0005] This application provides a string processing method, apparatus, device, and medium to solve the problems of high time complexity and low efficiency in string concatenation using the shortest possible method.
[0006] Firstly, this application provides a string processing method, including:
[0007] Determine the set of substrings;
[0008] The substrings in the substring set are stored in a trie, wherein the nodes in the trie are used to store the characters in the substrings, the substrings correspond one-to-one with the paths in the trie, and the common prefixes between different substrings correspond to the same path;
[0009] Take the first substring from the set of substrings and determine the initial string as the first substring;
[0010] Based on the trie, the remaining substrings in the substring set are matched with the initial string using the maximum prefix to obtain the second substring;
[0011] Take the second substring from the set of substrings, and concatenate the initial string with the second substring to obtain the concatenated string;
[0012] If the set of substrings is empty, then the target string is determined to be the concatenated string.
[0013] In one feasible implementation, after concatenating the initial string with the second substring to obtain the concatenated string, the method further includes:
[0014] If the substring set is not empty, the initial string is updated to the concatenated string, and the next maximum prefix matching operation and concatenation operation are performed until the substring set is empty.
[0015] In one feasible implementation, the step of performing maximum prefix matching between the remaining substrings in the substring set and the initial string according to the trie to obtain the second substring includes:
[0016] Based on the trie, the remaining substrings in the substring set are matched with the initial string using the maximum prefix matching method to obtain the maximum prefix matching substring of the initial string;
[0017] If the character matching length between the maximum prefix matching substring and the initial string is greater than zero, then the second substring is determined to be the maximum prefix matching substring;
[0018] If the character matching length between the longest prefix matching substring and the initial string is zero, then the second substring is obtained sequentially from the remaining substrings in the substring set.
[0019] In one feasible implementation, determining the set of substrings includes:
[0020] The original string is split to obtain the substring;
[0021] Based on the given substrings, the set of substrings is obtained;
[0022] The substring satisfies the following condition:
[0023] The character length of the substring is within the specified length range;
[0024] At least one prefix p of at least one of the substrings and a prefix of length |p|+i do not constitute the suffix of the remaining substrings in the set of substrings;
[0025] At least one suffix q of at least one of the substrings and suffixes of length |q|+i do not constitute prefixes of the remaining substrings in the set of substrings;
[0026] Where i is greater than or equal to 1.
[0027] In one feasible implementation, splitting the original string to obtain the substring includes:
[0028] According to the length range, the substring is randomly obtained multiple times from the original string;
[0029] Alternatively, the substring may be randomly selected multiple times from the original string according to the specified length range, and the substring may be selected from the original string according to a rule. The number of randomly selected substrings and the number of substrings selected according to the rule may be adjusted according to a weight parameter.
[0030] In one feasible implementation, the process of randomly obtaining the substring from the original string in one step includes:
[0031] Randomly determine the start and end positions;
[0032] If the difference between the start position and the end position is within the length range, then the character located between the start position and the end position is obtained from the original string;
[0033] The substring is obtained based on the characters located between the start position and the end position.
[0034] In one feasible implementation, the process of extracting the substring from the original string according to rules includes:
[0035] Identify characters in the original string that do not appear in the substring set;
[0036] The start position is determined based on the characters in the original string that do not appear in the substring set, and the end position is determined based on the start position and the length range.
[0037] Extract the characters located between the start position and the end position from the original string;
[0038] The substring is obtained based on the characters located between the start position and the end position.
[0039] Secondly, this application provides a string processing apparatus, comprising:
[0040] The set determination module is used to determine the set of substrings;
[0041] The tree generation module is used to store the substrings in the substring set into the trie, wherein the nodes in the trie are used to store the characters in the substrings, the substrings correspond one-to-one with the paths in the trie, and the common prefixes between different substrings correspond to the same path;
[0042] The initial string determination module is used to extract the first substring from the substring set and determine the initial string as the first substring;
[0043] The prefix matching module is used to perform maximum prefix matching between the remaining substrings in the substring set and the initial string according to the trie to obtain the second substring;
[0044] The concatenation module is used to extract the second substring from the substring set, and concatenate the initial string with the second substring to obtain the concatenated string;
[0045] The target string determination module is used to determine the target string as the concatenated string if the substring set is empty.
[0046] Thirdly, this application provides an electronic device, including: at least one processor and a memory;
[0047] The memory stores computer-executed instructions;
[0048] The at least one processor executes computer execution instructions stored in the memory, causing the at least one processor to perform the string processing method as described in the first aspect above.
[0049] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the string processing method described in the first aspect above.
[0050] Fifthly, this application provides a computer program product, which includes a computer program that, when executed by a processor, implements the string processing method described in the first aspect above.
[0051] The string processing method, apparatus, device, and medium provided in this application store substrings from a substring set in a trie. The nodes of the trie store characters from the substrings, and each substring corresponds one-to-one with a path in the trie. Common prefixes between different substrings correspond to the same path. During character concatenation, the remaining substrings in the substring set are matched with the initial string using the trie to obtain a second substring. The initial string is then concatenated with the second substring to obtain the concatenated string. Thus, the trie structure is used to achieve maximum prefix matching, thereby realizing string concatenation. The trie structure trades space for time, reducing the time complexity of maximum prefix matching and improving its efficiency, which in turn reduces the time complexity of string concatenation and improves its efficiency. Attached Figure Description
[0052] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0053] Figure 1 Flowchart of the string processing method provided in the embodiments of this application Figure 1 ;
[0054] Figure 2 Example diagram of the trie structure provided in the embodiments of this application;
[0055] Figure 3 Flowchart of the string processing method provided in the embodiments of this application Figure 2 ;
[0056] Figure 4 Flowchart of the string processing method provided in the embodiments of this application Figure 3 ;
[0057] Figure 5 This is a schematic diagram of the structure of the string processing device provided in the embodiments of this application;
[0058] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.
[0059] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0060] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0061] First, to facilitate understanding of this solution, some of the terms used in this application will be explained:
[0062] A string is a sequence of characters consisting of one or more of the following classes: numbers, letters, and underscores.
[0063] Substring: k consecutive characters starting from the j-th position of the string.
[0064] String matching: Searches a longer string for the specified string and finds the position of the specified string within that longer string.
[0065] String concatenation: A new string formed by concatenating two or more strings.
[0066] Prefix: The first k characters of the string.
[0067] Maximum prefix matching: A string can be matched with multiple substrings, and the longest substring is called the maximum prefix matching string. This matching process is called maximum prefix matching.
[0068] A trie (also known as a prefix tree, word lookup tree, or key tree) is a multi-way tree structure. For example, a trie for English letters is a 26-way tree, and a trie for numbers is a 10-way tree.
[0069] Time complexity: It is expressed as a time complexity function, used to qualitatively describe the running time of an algorithm.
[0070] Space complexity: A measure of the amount of temporary storage space occupied by an algorithm during its execution.
[0071] Space for time: Try to trade more memory or disk space for computing or network resources, thereby increasing the system's memory consumption to speed up program execution.
[0072] In related technologies, string concatenation methods directly use the "+" (addition) operation. This operation can only perform simple concatenation and cannot merge the common strings between two strings to form the shortest possible merged string. For example, according to the shortest string concatenation method, the strings "ABCD" and "CDEF" should be merged into the string "ABCDEF", not "ABCDCDEF".
[0073] There is currently a lack of methods for concatenating multiple strings to obtain the shortest possible concatenation string, but the need for this approach exists. For example, software development may require shortest string concatenation; encryption and decryption may also require shortest string concatenation to obtain the correct key; and in genome sequence assembly tasks, short fragments generated from sequencing need to be assembled to reconstruct the original sequence, a process that also requires shortest string concatenation.
[0074] The concatenation requirement for the shortest concatenation string can be described as follows: Given N strings of length (len1, len2), concatenate them while ensuring that the current string matches the longest prefix of the next selected string. After concatenating these N strings, the shortest concatenation string is obtained.
[0075] To address this concatenation requirement, either brute-force matching or the KMP (Knuth-Morris-Pratt) matching method can be used to select the substring with the longest prefix match to the current string. First, an arbitrary substring can be selected from the set of substrings. Then, from the remaining substrings in the set, either brute-force matching or KMP matching is used to select a substring with the longest prefix match and concatenate it with the randomly selected substring to ensure the shortest possible concatenated string. This process of performing longest prefix matching and concatenation multiple times is repeated to obtain the final concatenated string.
[0076] The main process of the brute-force matching method is as follows: First, compare the character at a specified position in the main string with the character at the beginning position of the target string; if they are the same, compare the next character in the main string with the next character in the target string; if they are different, compare the next character in the main string with the character at the beginning position of the target string. This continues until every character in the target string matches a consecutive character in the main string, determining a match; otherwise, a mismatch is determined.
[0077] The main process of the KMP method is to find a substring in the main string that is equal to the target string. The core is to build a state transition table based on the target string, and this state transition table does not depend on the main string.
[0078] However, using the above two methods to concatenate N strings to find the shortest concatenated string has a very high time complexity. Imagine concatenating N strings of length (len1, len2) (assuming the average string length is m). The specific process is as follows: First, randomly select a string from the N strings and use it as the initial string Q. The time efficiency of concatenation at this time is O(1). Then, match the largest suffix of Q with the other N-1 strings to obtain the largest prefix of the remaining N-1 strings relative to the partial string Q, and check if they can match. The time complexity at this time is O(N*m*m). After performing these operations multiple times, the concatenated string becomes longer and longer, and the number of comparisons increases. Exhaustively searching all possibilities becomes a very time-consuming task.
[0079] To address the aforementioned shortcomings, this application provides a string processing method, apparatus, device, and medium. In this application, substrings are stored in a trie (prefix tree), which is used to implement maximum prefix matching and shortest string concatenation. Since the core idea of a trie is to trade space for time, the common prefixes of strings can be used to reduce unnecessary string comparisons, thereby improving query efficiency, which in turn improves the efficiency of maximum prefix matching, reduces the time complexity of maximum prefix matching, and consequently reduces the time complexity of shortest string concatenation, thus improving the efficiency of shortest string concatenation.
[0080] The advantages of a trie are as follows: 1. Both string insertion and string retrieval are highly efficient, with a time complexity of O(m), where m is the length of the string to be inserted / retrieved; 2. Compared to hash structures, different keys in a trie will not cause conflicts, improving query accuracy; 3. Tries can sort keys lexicographically.
[0081] In one example, the string processing method provided in this application embodiment can be applied to a development scenario, which involves the shortest string concatenation.
[0082] In another example, the string processing method provided in this application embodiment can be applied to the scenario of genome sequence assembly. In this scenario, the genome sequence can be described as a string, and then the string processing method provided in this application embodiment can be used for string assembly. Genome sequence assembly is the most fundamental task in genome sequence analysis and is crucial to the success or failure of genome research. Applying the string processing method provided in this application embodiment to this task is of great significance.
[0083] The string processing method provided in this application is executed by a string processing device, which can be a terminal or a server.
[0084] The terminal can be a personal digital assistant (PDA) device, a handheld device (such as a smartphone or tablet), a computing device (such as a personal computer (PC)), an in-vehicle device, a wearable device (such as a smartwatch or smart bracelet), or a smart home device (such as a smart display device). The server can be a distributed server, a centralized server, or a cloud server.
[0085] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0086] refer to Figure 1 , Figure 1 Flowchart of the string processing method provided in the embodiments of this application Figure 1 .
[0087] like Figure 1 As shown, the string processing method provided in this embodiment includes:
[0088] S101. Determine the set of substrings.
[0089] The substring set includes multiple substrings, which are substrings that belong to the same original string.
[0090] In this embodiment, a set of substrings input by the user can be obtained; or, a set of substrings can be obtained from a database; or, a set of substrings sent by other devices can be received; or, the original string can be split to obtain substrings, and a set of substrings can be obtained based on the substrings, that is, multiple substrings obtained from splitting the original string can be combined into a set of substrings.
[0091] S102. Store the substrings in the substring set into a trie. The nodes in the trie are used to store the characters in the substrings. The substrings correspond one-to-one with the paths in the trie. The common prefixes between different substrings correspond to the same path.
[0092] In this embodiment, the trie is an algorithmic structure. First, a trie can be constructed using a trie building algorithm. Then, each substring in the substring set is stored in the trie using an insertion algorithm. The trie construction algorithm and the insertion algorithm for inserting strings into the trie can be found in existing algorithms and will not be elaborated here.
[0093] As an example, Figure 2 This is an example diagram of the structure of a trie provided in an embodiment of this application. Figure 2As shown, the set of substrings stored in the trie is {"a", "to", "tea", "ted", "ten", "inn"}. In the trie, each substring is stored on a separate path, with a one-to-one correspondence between the substring and the path in the trie, rather than being stored in a single node. Furthermore, two substrings with a common prefix share the same path for their common prefix; that is, the common prefixes of different substrings correspond to the same path. For example, the common prefix "te" of the substrings "tea", "ted", and "ten" lies on the same path.
[0094] S103. Take the first substring from the substring set and determine the initial string as the first substring.
[0095] In this embodiment, the first substring can be extracted from the substring set according to the order of the substrings in the set; that is, the first substring is the first substring in the substring set. Extracting the first substring from the substring set is done without replacement, meaning the first substring is retrieved from the substring set and then deleted from it. The initial string refers to the initial string used for concatenating the strings. Determining the initial string as the first substring means selecting the first string in the substring set as the initial string for concatenation.
[0096] After retrieving the first substring, it can be determined whether the substring set is empty. If the substring set is empty, the target string can be determined to be the first substring, and no further concatenation is needed. If the substring set is not empty, S104 is executed to perform substring concatenation until the substring set is empty.
[0097] S104. Based on the trie, perform maximum prefix matching between the remaining substrings in the substring set and the initial string to obtain the second substring.
[0098] The second substring can be the maximum prefix matching substring obtained by maximum prefix matching.
[0099] In this embodiment, based on the trie, the remaining substrings in the substring set can be subjected to multiple maximum prefix matches with the initial string. The maximum prefix matching process may include: first, determining the suffix set of the initial string; then, obtaining the maximum suffix from the suffix set of the initial string and deleting it from the suffix set; next, searching in the trie for a substring whose prefix includes the maximum suffix (i.e., determining whether the maximum suffix is the maximum prefix of the remaining substrings); if it exists, the substring can be determined to be a maximum prefix matching substring; otherwise, the process can jump to obtaining the maximum suffix from the suffix set of the initial string. The maximum suffix is the longest suffix in the suffix set of the initial string.
[0100] As an example, suppose the substring set is Sub = {ABCDE, DEF, ABCD, FAG}. First, take the first substring, which equals Q, i.e., Q = ABCDE. Then Sub = {DEF, ABCD, FAG}, where Q represents the initial string. Determine that the suffix set of Q includes {ABCDE, BCDE, CDE, DE, E}. Take the largest suffix "ABCDE" from the suffix set of Q. In the trie, search if there exists a substring in the Sub set with a prefix containing "ABCDE", and the result is no. Take the largest suffix "BCDE" from the suffix set of Q. In the trie, search if there exists a substring in the Sub set with a prefix containing "BCDE", and the result is no. Repeat this matching search multiple times until we find that the largest suffix "DE" of Q is a largest prefix of the substring "DEF" in the Sub set. Finally, the largest prefix matching substring of Q is determined to be "DEF".
[0101] S105. Take the second substring from the substring set, and concatenate the initial string with the second substring to obtain the concatenated string.
[0102] In this embodiment, the second substring can be deleted from the substring set, leaving only the unconcatenated substrings in the set. The initial string is then concatenated with the second substring, and the common strings between the suffix of the initial string and the prefix of the second substring are merged to obtain the concatenated string.
[0103] S106. If the substring set is empty, then the target string is determined to be the concatenated string.
[0104] In this embodiment, after extracting the second substring, if the substring set is empty, it means that there is no string in the substring set that has not been concatenated, that is, all substrings in the substring set have been concatenated. Therefore, the target string is determined to be the concatenated string obtained by concatenating the initial string and the second substring.
[0105] In this embodiment, a trie structure is used to trade space for time, reducing the time complexity of maximum prefix matching between the main string (i.e., the initial string) and multiple target strings (i.e., multiple substrings in the substring set), thus improving the efficiency of maximum prefix matching. Furthermore, this reduces the time complexity of shortest concatenation of multiple substrings, improving the efficiency of shortest concatenation of multiple substrings.
[0106] refer to Figure 3 , Figure 3 Flowchart of the string processing method provided in the embodiments of this application Figure 2 .
[0107] like Figure 3 As shown, the string processing method provided in this embodiment includes:
[0108] S301. Determine the set of substrings.
[0109] S302. Store the substrings in the substring set into a trie. The nodes in the trie are used to store the characters in the substrings. The substrings correspond one-to-one with the paths in the trie. The common prefixes between different substrings correspond to the same path.
[0110] S303. Take the first substring from the substring set and determine the initial string as the first substring.
[0111] The implementation principles and technical effects of S301 to S303 can be referred to in the aforementioned embodiments, and will not be repeated here.
[0112] S304. Based on the trie, perform maximum prefix matching between the remaining substrings in the substring set and the initial string to obtain the second substring.
[0113] The process of performing maximum prefix matching between the remaining substrings in the substring set and the initial string according to the trie can be referred to the aforementioned embodiment and will not be repeated here.
[0114] In one feasible implementation, S304 includes: according to the trie, performing maximum prefix matching between the remaining substrings in the substring set and the initial string to obtain the maximum prefix matching substring of the initial string; if the character matching length between the maximum prefix matching substring and the initial string is greater than zero, then determining the second substring as the maximum prefix matching substring; if the character matching length between the maximum prefix matching substring and the initial string is zero, then sequentially obtaining the second substring from the remaining substrings in the substring set.
[0115] In this implementation, based on the description of the maximum prefix matching process above, it can be seen that: if the character matching length between the maximum prefix matching substring and the initial string is greater than zero, it means that the prefix of the maximum prefix matching substring contains the maximum suffix of the initial string; if the character matching length between the maximum prefix matching substring and the initial string is equal to zero, it means that the prefix of the maximum prefix matching substring does not contain the maximum suffix of the initial string. Therefore, when the character matching length between the maximum prefix matching substring and the initial string is greater than zero, concatenating the maximum prefix matching substring and the initial string achieves the shortest concatenation; when the character matching length between the maximum prefix matching substring and the initial string is equal to zero, the second substring is obtained from the substring set according to the order of substrings in the substring set.
[0116] This allows for full consideration of different matching results and improves the accuracy of string concatenation.
[0117] S305. Take the second substring from the substring set, and concatenate the initial string with the second substring to obtain the concatenated string.
[0118] The implementation principle and technical effects of S305 can be referred to in the aforementioned embodiments, and will not be repeated here.
[0119] In one feasible implementation, when the character matching length between the longest prefix matching substring and the initial string is equal to zero, that is, when the character matching length between the second substring and the initial string is equal to zero, it can be determined whether the second substring is contained in the initial string, that is, whether the second substring belongs to a substring of the initial string. If the second substring is contained in the initial string, then the initial string and the second substring are not concatenated; otherwise, the initial string and the second substring are concatenated.
[0120] S306. Determine whether the substring set is empty.
[0121] In this embodiment, it is determined whether the substring set is empty. If the substring set is empty, it means that all the substrings in the substring set have been concatenated, and S308 is executed; if the substring set is not empty, it means that there are still substrings in the substring set that have not been concatenated, and S307 is executed.
[0122] S307. Update the initial string to the concatenated string.
[0123] In this embodiment, after updating the initial string to the concatenated string, the process can jump to S304 to continue executing the next maximum prefix matching operation and concatenation operation until the substring set is empty.
[0124] S308. Determine the target string as a concatenated string.
[0125] In this embodiment, the final target string is obtained by concatenating all the substrings in the substring set.
[0126] As an example, suppose the set of substrings is Sub = {ABCDE, DEF, ABCD, FAG}:
[0127] First, let the first substring equal to Q, i.e., Q = ABCDE. Then Sub = {DEF, ABCD, FAG}, where Q represents the initial string. The suffix set of Q includes {ABCDE, BCDE, CDE, DE, E}. Take the largest suffix "ABCDE" from the suffix set of Q. In the trie, search for a substring in Sub containing the prefix "ABCDE"; the result is no. Take the largest suffix "BCDE" from the suffix set of Q. In the trie, search for a substring in Sub containing the prefix "BCDE"; the result is no. Repeat this matching search multiple times until we find that the largest suffix "DD" of Q is a largest prefix of the substring "DEF" in the Sub set. The largest prefix matching substring of Q is determined to be "DEF". Concatenate Q with "DEF" to get Q = ABCDEF. Remove "DEF" from Sub to get Sub = {ABCD, FAG}.
[0128] Next, since Sub = {ABCD, FAG} is not empty, we continue to perform maximum prefix matching between Q and multiple substrings in Sub, and concatenate the strings based on the maximum prefix matching results. The specific process will not be described in detail. This process is repeated until Sub is empty.
[0129] In this embodiment, a trie structure is used to trade space for time, reducing the time complexity of maximum prefix matching between the main string (i.e., the initial string) and multiple target strings (i.e., multiple substrings in the substring set), thus improving the efficiency of maximum prefix matching. Furthermore, this reduces the time complexity of shortest concatenation of multiple substrings, improving the efficiency of shortest concatenation. Moreover, through multiple maximum prefix matches and string concatenations, the accuracy of string concatenation is improved.
[0130] refer to Figure 4 , Figure 4 Flowchart of the string processing method provided in the embodiments of this application Figure 3 .
[0131] like Figure 4 As shown, the string processing method provided in this embodiment includes:
[0132] S401. Split the original string to obtain substrings.
[0133] In the genome sequence assembly scenario, the original string can be the original genome sequence; in the string assembly verification scenario (i.e., verifying the shortest assembly effect of this scheme), the original string can be a randomly generated string of length L, where L can be a preset value or a random value.
[0134] This can involve: randomly generating a raw string; retrieving a raw string from a database; obtaining a raw string input by the user; or receiving a raw string sent by other devices.
[0135] In this embodiment, the original string S is split into N substrings with character lengths within the range (len1, len2). N represents the number of substrings, and (len1, len2) represents the length range. The set of substrings is denoted by Sub, and each substring in the set can be represented by s_i, where i represents the i-th substring split from the original string S. Therefore, Sub = {s_1, s_2, s_3…s_(N-1), s_N}.
[0136] In order for all the substrings to be successfully reassembled into the original string, the resulting substrings must satisfy the following conditions: Condition 1: The character length of the substring is within the specified range; Condition 2: At least one prefix p of at least one substring and a prefix of length |p|+i do not constitute suffixes of the remaining substrings in the substring set; Condition 3: At least one suffix q of at least one substring and a suffix of length |q|+i do not constitute prefixes of the remaining substrings in the substring set. i is greater than or equal to 1.
[0137] The expanded descriptions of conditions 2 and 3 are as follows: There exists at least one substring whose at least one prefix p and a prefix of length |p|+i do not constitute the suffixes of the remaining substrings in the substring set; There exists at least one substring whose at least one suffix q and a suffix of length |q|+i do not constitute the prefixes of the remaining substrings in the substring set, where i is greater than or equal to 1.
[0138] Condition 2, for example: A = "qazwsx", whose prefixes include "q", "qa", "qaz", "qazw", "qazws", and "qazwsx". Suppose there exists p = "qazw" and i = 2, that is, "qazw", "qazws", and "qazwsx" do not constitute suffixes for other strings;
[0139] Condition 3, for example: B = "LWXRSIW", with suffixes "LWXRSIW", "WXRSIW", "XRSIW", "RSIW", "SIW", "IW", and "W". Suppose there exist q = "RSIW" and i = 2, that is, "WXRSIW", "XRSIW", and "RSIW" do not constitute prefixes for other strings.
[0140] Thus, the substrings generated from the original string S may overlap or be substrings of other strings, resulting in a variety of substrings. Furthermore, if the Sub set contains substrings that begin or end with strings A and B, it ensures that the shortest string is not concatenated using any arbitrary substring as the beginning or end, thereby improving the accuracy of string concatenation.
[0141] The original string S can be split randomly or according to a certain ratio rule. See the following implementation for details:
[0142] In one feasible implementation, S401 includes: randomly selecting a substring multiple times from the original string according to a length range; or, randomly selecting a substring multiple times from the original string according to a length range, and selecting a substring from the original string according to a rule, wherein the number of randomly selected substrings and the number of substrings selected according to the rule are adjusted according to a weight parameter.
[0143] In this implementation, substrings are randomly selected multiple times from the original string according to a length range, ensuring that the character length of the selected substrings meets the length range. Considering that random substring selection might result in characters in the original string not appearing in any substring (i.e., one or more characters in the original string might be missed), leading to inaccurate final concatenated strings, this method addresses this issue by randomly selecting substrings multiple times from the original string according to a length range, while simultaneously selecting substrings from the original string according to rules. These rules ensure that each character is contained in more than one substring, and the randomness ensures the diversity and randomness of the substrings.
[0144] In the process of adjusting the number of randomly obtained substrings and the number of substrings obtained according to the rules based on the weight parameters, substrings can be randomly obtained multiple times from the original string according to the length range. Then, the proportion of characters contained in the randomly obtained substrings in the original string is determined. If the proportion is greater than or equal to the weight parameters, substrings are obtained from the original string according to the rules. Otherwise, substrings are randomly obtained from the original string multiple times according to the length range.
[0145] As an example, the weight parameter is Random_Percentage, which ranges from 0 to 1. If Random_Percentage = 0, all substrings are generated by splitting the original string according to the rules; if Random_Percentage = 0.1, substrings are randomly selected from the original string. If 10% of the characters in the original string S are already contained in the randomly selected substring, the generated substrings are no longer random, but are generated with slightly more rules.
[0146] In a feasible implementation manner, the process of randomly obtaining substrings from the original string once includes: randomly determining the start position and the end position; if the difference between the start position and the end position is within the length range, obtaining the characters between the start position and the end position in the original string; and obtaining the substring according to the characters between the start position and the end position.
[0147] In this implementation manner, the random function (random function) is used to obtain the start position k_start and the end position k_end. If len1 < k_end - k_start < len2, then the string between (k_start, k_end) is obtained from the original string S as a substring, and this operation is repeated multiple times until each character in the original string S is included in more than one substring. The longer the original string S is, the more substrings are split, and the longer the time consumed is.
[0148] In a feasible implementation manner, the process of obtaining substrings from the original string according to rules includes: determining the characters that do not appear in the substring set in the original string; determining the start position according to the characters that do not appear in the substring set in the original string, and determining the end position according to the start position and the length range; obtaining the characters between the start position and the end position in the original string; and obtaining the substring according to the characters between the start position and the end position.
[0149] In this embodiment, the characters that do not appear in the substring set are determined in the original string, that is, the characters in the original string that are not included in the substring; the character position of this character in the original string is determined, and the first random value is subtracted from the character position to obtain the start position k_start. Then, the second random value is added to the start position k_start to obtain the end position k_end. Among them, the second random value is within the length range (len1, len2). In the original string, the substring between the start position k_start and the end position k_end is obtained, and this operation is repeated multiple times until each character in the original string is included in more than one substring. In this way, compared with the method of purely randomly obtaining substrings, this solution can generate the required substrings more quickly and meet the condition that each character in the original string is included in more than one substring.
[0150] S402. Obtain a substring set according to the substrings.
[0151] In this embodiment, the substrings are combined into a substring set.
[0152] S403. Store the substrings in the substring set into a trie. The nodes in the trie are used to store the characters in the substrings. The substrings correspond one-to-one with the paths in the trie. The common prefixes between different substrings correspond to the same path.
[0153] S404. Take the first substring from the substring set and determine the initial string as the first substring.
[0154] S405. Based on the trie, perform maximum prefix matching between the remaining substrings in the substring set and the initial string to obtain the second substring.
[0155] S406. Take the second substring from the substring set, and concatenate the initial string with the second substring to obtain the concatenated string.
[0156] S407. If the substring set is empty, then the target string is determined to be the concatenated string.
[0157] The implementation principles and technical effects of S403 to S407 can be referred to in the aforementioned embodiments, and will not be repeated here.
[0158] In this embodiment, the original string is split into substrings, and then a trie structure is used to perform maximum prefix matching and string concatenation on the substrings, so that the substrings can be concatenated back into the original string. This method can be applied to scenarios such as genome sequence assembly and string concatenation verification.
[0159] It should be noted that there is a possibility that the original string cannot be reconstructed in the process of this invention. The reason for the error is not due to a flaw in the algorithm, but rather to the existence of the randomly generated original string S and the randomly generated substring set Sub themselves.
[0160] Suppose we have an original string S = ABCDEFABCDFAG. If we randomly divide it into substrings resulting in Sub = {ABCDE, DEF, ABCD, FAG}, then the operation to reconstruct the substrings is as follows:
[0161] First, take the first substring that equals Q, i.e., Q = ABCDE, then Sub = {DEF, ABCD, FAG};
[0162] We search for whether the longest suffix of Q, “ABCDE”, is the longest prefix of the rest of the substrings. The result is no. We search for whether the longest suffix of Q, “BCDE”, is the longest prefix of the rest of the substrings. The result is no. By comparing them in turn, we can find that the longest suffix of Q, DE, is a longest prefix of the substring DEF in Sub. Therefore, Q = Q + DEF, that is, Q = ABCDEF. At the same time, Sub = {ABCD, FAG}.
[0163] Repeating the above steps, we can obtain that the suffix F of Q is a maximum prefix of the substring FAG, so Q = Q + FAG, which is "ABCDEFAG", and Sub = {ABCD}
[0164] Continuing, iterate through each suffix of Q, checking if the suffix is a prefix of a substring in Sub, and if so, check if the substring "ABCD" in Sub is a substring of Q. If not, append it directly to Q; if so, no appending is needed, and the concatenation ends. The answer here is that the substring "ABCD" is a subset of Q. Therefore, the final Q = ABCDEFAG. However, S = ABCDEFABCDFAG, while Q = ABCDEFAG, so S is not equal to Q, and this concatenation verification fails.
[0165] The reason for the failure is that the original string S contains two identical subsets "ABCD". The substring splitting process happens to separate these two substrings. Since we assume that two substrings can be contained within each other during substring splitting, it mistakenly assumes that substrings "ABCDE" and "ABCD" are at the same position, leading to the concatenation failure. However, it's worth noting that because the substring lengths in this example are within a certain range, the probability of splitting both identical substrings when the original string contains them is very small. Therefore, the possibility of the above concatenation error is also very small.
[0166] The following are embodiments of the apparatus described in this application, which can be used to execute the corresponding method embodiments of this application. For details not disclosed in the apparatus embodiments of this application, please refer to the corresponding method embodiments of this application.
[0167] Figure 5 This is a schematic diagram of the structure of a string processing device provided in an embodiment of this application. Figure 5 As shown, the string processing device 500 provided in this embodiment includes:
[0168] Set determination module 501 is used to determine the set of substrings;
[0169] The tree generation module 502 is used to store substrings in the substring set into a trie. The nodes in the trie are used to store the characters in the substrings. The substrings correspond one-to-one with the paths in the trie, and the common prefixes between different substrings correspond to the same path.
[0170] The initial string determination module 503 is used to extract the first substring from the substring set and determine the initial string as the first substring;
[0171] The prefix matching module 504 is used to perform maximum prefix matching between the remaining substrings in the substring set and the initial string based on the trie to obtain the second substring;
[0172] The concatenation module 505 is used to extract the second substring from the substring set, concatenate the initial string with the second substring, and obtain the concatenated string.
[0173] The target string determination module 506 is used to determine the target string as a concatenated string if the substring set is empty.
[0174] In one feasible implementation, the string processing device 500 further includes a loop module 507, which updates the initial string to the concatenated string if the substring set is not empty, and continues to perform the next maximum prefix matching operation and concatenation operation until the substring set is empty.
[0175] In one feasible implementation, the prefix matching module 504 is specifically used to: perform maximum prefix matching between the remaining substrings in the substring set and the initial string according to the trie, to obtain the maximum prefix matching substring of the initial string; if the character matching length between the maximum prefix matching substring and the initial string is greater than zero, then the second substring is determined to be the maximum prefix matching substring; if the character matching length between the maximum prefix matching substring and the initial string is zero, then the second substring is obtained sequentially from the remaining substrings in the substring set.
[0176] In one feasible implementation, the set determination module 501 is specifically used for: splitting the original string to obtain substrings; and obtaining a set of substrings based on the substrings. The substrings satisfy the following conditions: the character length of the substring is within a certain range; at least one prefix p of at least one substring and a prefix of length |p|+i do not constitute suffixes of the remaining substrings in the substring set; at least one suffix q of at least one substring and a suffix of length |q|+i do not constitute prefixes of the remaining substrings in the substring set; and i is greater than or equal to 1. In more detail: there exists at least one substring whose at least one prefix p and a prefix of length |p|+i do not constitute suffixes of the remaining substrings in the substring set; and at least one substring whose at least one suffix q and a suffix of length |q|+i do not constitute prefixes of the remaining substrings in the substring set, and i is greater than or equal to 1.
[0177] In one feasible implementation, during the process of splitting the original string to obtain substrings, the set determination module 501 is specifically used to: randomly select substrings multiple times from the original string according to the length range; or, randomly select substrings multiple times from the original string according to the length range, and select substrings from the original string according to the rules, wherein the number of randomly selected substrings and the number of substrings selected according to the rules are adjusted according to the weight parameter.
[0178] In one feasible implementation, the process of randomly obtaining a substring from the original string is specifically performed by the set determination module 501 as follows: randomly determining the start position and the end position; if the difference between the start position and the end position is within the length range, then obtaining the characters located between the start position and the end position in the original string; and obtaining the substring based on the characters located between the start position and the end position.
[0179] In one feasible implementation, during the process of obtaining substrings from the original string according to rules, the set determination module 501 is specifically used to: determine characters in the original string that do not appear in the substring set; determine the start position based on the characters in the original string that do not appear in the substring set, and determine the end position based on the start position and the length range; obtain characters in the original string that are located between the start position and the end position; and obtain the substring based on the characters located between the start position and the end position.
[0180] It is worth noting that the string processing apparatus provided in the above embodiments can be used to execute each step in the string processing method provided in any of the above embodiments. The specific implementation methods and technical effects are similar, and will not be repeated here.
[0181] The device embodiments provided in this application are merely illustrative, and the module division is only a logical functional division. In actual implementation, there may be other division methods. For example, multiple modules may be combined or integrated into another system. The coupling between the modules can be achieved through interfaces, which are usually electrical communication interfaces, but mechanical interfaces or other forms of interfaces are also possible. Therefore, the modules described as separate components may or may not be physically separate; they may be located in one place or distributed in different locations on the same or different devices.
[0182] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 6 As shown, the electronic device 600 may include at least one processor 601 and a memory 602. Figure 6 The example shown is an electronic device using a processor.
[0183] The memory 602 is used to store the program of the processor 601. Specifically, the program may include program code, which includes computer operation instructions.
[0184] The memory 602 may include high-speed RAM memory, and may also include non-volatile memory, such as at least one disk storage device.
[0185] The processor 601 is configured to execute a computer program stored in the memory 602 to implement the steps of the string processing methods in the above method embodiments.
[0186] The processor 601 may be a central processing unit (CPU), an application-specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of this application.
[0187] Optionally, the memory 602 can be either independent or integrated with the processor 601. When the memory 602 is a device independent of the processor 601, the electronic device 600 may further include a bus 603 for connecting the processor 601 and the memory 602. The bus can be an industry standard architecture (ISA) bus, a peripheral component (PCI) bus, or an extended industry standard architecture (EISA) bus, etc. Buses can be classified as address buses, data buses, control buses, etc., but this does not mean that there is only one bus or one type of bus.
[0188] Optionally, in a specific implementation, if the memory 602 and the processor 601 are integrated on a single chip, then the memory 602 and the processor 601 can communicate through an internal interface.
[0189] This application also provides a computer-readable storage medium, which may include various media capable of storing program code, such as a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk. Specifically, the computer-readable storage medium stores computer-executable instructions. When at least one processor of the electronic device executes the computer-executable instructions, the electronic device executes the various steps of the string processing methods provided in the above-described embodiments.
[0190] This application also provides a computer program product comprising a computer program stored in a readable storage medium. At least one processor of an electronic device can read the computer program from the readable storage medium, and the at least one processor executes the computer program to cause the electronic device to perform the various steps of the string processing methods provided in the above-described embodiments.
[0191] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.
[0192] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A string processing method, characterized in that, include: The original string is split to obtain substrings; a set of substrings is obtained based on the substrings; wherein the substrings satisfy the following conditions: the character length of the substring is within a certain range; at least one prefix p of the substring and a length of is obtained. The prefixes do not constitute suffixes of the remaining substrings in the substring set; at least one suffix q of at least one substring and a length of The suffixes do not constitute the prefixes of the remaining substrings in the substring set; i is greater than or equal to 1; The substrings in the substring set are stored in a trie, wherein the nodes in the trie are used to store the characters in the substrings, the substrings correspond one-to-one with the paths in the trie, and the common prefixes between different substrings correspond to the same path; According to the order of the substrings in the substring set, the first substring is taken out from the substring set, and the initial string is determined as the first substring. The first substring is taken out without replacement. Based on the trie, the remaining substrings in the substring set are matched with the initial string using the maximum prefix to obtain the second substring; Take the second substring from the set of substrings, and concatenate the initial string with the second substring to obtain the concatenated string; If the set of substrings is empty, then the target string is determined to be the concatenated string.
2. The string processing method according to claim 1, characterized in that, After concatenating the initial string with the second substring to obtain the concatenated string, the process further includes: If the substring set is not empty, the initial string is updated to the concatenated string, and the next maximum prefix matching operation and concatenation operation are performed until the substring set is empty.
3. The string processing method according to claim 2, characterized in that, The step of performing maximum prefix matching between the remaining substrings in the substring set and the initial string according to the trie to obtain the second substring includes: Based on the trie, the remaining substrings in the substring set are matched with the initial string using the maximum prefix matching method to obtain the maximum prefix matching substring of the initial string; If the character matching length between the maximum prefix matching substring and the initial string is greater than zero, then the second substring is determined to be the maximum prefix matching substring; If the character matching length between the longest prefix matching substring and the initial string is zero, then the second substring is obtained sequentially from the remaining substrings in the substring set.
4. The string processing method according to claim 1, characterized in that, The process of splitting the original string to obtain the substring includes: According to the length range, the substring is randomly obtained multiple times from the original string; Alternatively, the substring may be randomly selected multiple times from the original string according to the specified length range, and the substring may be selected from the original string according to a rule. The number of randomly selected substrings and the number of substrings selected according to the rule may be adjusted according to a weight parameter.
5. The string processing method according to claim 4, characterized in that, The process of randomly obtaining the substring from the original string includes: Randomly determine the start and end positions; If the difference between the start position and the end position is within the length range, then the character located between the start position and the end position is obtained from the original string; The substring is obtained based on the characters located between the start position and the end position.
6. The string processing method according to claim 4, characterized in that, The process of extracting the substring from the original string according to the rules includes: Identify characters in the original string that do not appear in the substring set; The start position is determined based on the characters in the original string that do not appear in the substring set, and the end position is determined based on the start position and the length range. Extract the characters located between the start position and the end position from the original string; The substring is obtained based on the characters located between the start position and the end position.
7. A string processing device, characterized in that, include: A set determination module is used to split the original string to obtain substrings; and to obtain a set of substrings based on the substrings; wherein the substrings satisfy the following conditions: the character length of the substring is within a certain range; at least one prefix p of the substring and a length of 1 / 2... The prefixes do not constitute suffixes of the remaining substrings in the substring set; at least one suffix q of at least one substring and a length of The suffixes do not constitute the prefixes of the remaining substrings in the substring set; i is greater than or equal to 1; The tree generation module is used to store the substrings in the substring set into a trie, wherein the nodes in the trie are used to store the characters in the substrings, the substrings correspond one-to-one with the paths in the trie, and the common prefixes between different substrings correspond to the same path; The initial string determination module is used to extract the first substring from the substring set according to the order of the substrings in the substring set, and determine the initial string as the first substring, wherein extracting the first substring from the substring set is done without replacement; The prefix matching module is used to perform maximum prefix matching between the remaining substrings in the substring set and the initial string according to the trie to obtain the second substring; The concatenation module is used to extract the second substring from the substring set, and concatenate the initial string with the second substring to obtain the concatenated string; The target string determination module is used to determine the target string as the concatenated string if the substring set is empty.
8. An electronic device, comprising: At least one processor and memory; The memory stores computer-executed instructions; The at least one processor executes computer execution instructions stored in the memory, causing the at least one processor to perform the string processing method as described in any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the string processing method as described in any one of claims 1 to 6.