Method sharing: Ordered Set Segmentation

Source: Internet
Author: User

1. Method Function Description

Ordered Set: (A, B, C, D, E, F, G, H, I, J), there are already segments (only the beginning and end): (A, D ), (G, H ).

Function: Find other segments (D, G), (H, J ).

For example, in the competition track, from location A to location J, it must pass through B, C, D, E, F, G, H, I, J, divide the orbit into several segments (with only the beginning and end): (A, D), (D, G), (G, H), (H, J ). Now we already have segments (A, D), (D, G). How can we find segments (G, H), (H, J )?

2. Code Description and description

(1) thoughts

Loop string, which compares to the first character of the substring one by one,

If the sub-string list does not exist, add the Sub-string and set the first character of the sub-string to this string;

If this character exists in the substring, the last character of the substring is obtained and the segment is skipped cyclically.

(2) C # code

 1        private List<List<string>> FillMidList(List<string> stringList, List<List<string>> midList)
2 {
3 List<List<string>> rtn = new List<List<string>>();
4 List<string> strList = null;//Char List
5 string start = string.Empty;//Start Char
6 string end = string.Empty;//End Char
7 bool isFind = false;
8 bool isFindEnd = false;//Find End Char
9
10 for (int i = 0; i < stringList.Count; i++)
11 {
12 start = stringList[i];
13 //If exit char
14 foreach (var item in midList)
15 {
16 if (start == item[0])
17 {
18 if (isFindEnd == true)
19 {
20 end = start;
21 }
22 isFind = true;
23 start = item[item.Count - 1];
24 break;
25 }
26 }
27
28 if (isFind == true)
29 {
30 isFind = false;
31 //Adjust index
32 for (int j = 0; j < stringList.Count; j++)
33 {
34 if (stringList[j] == start)
35 {
36 i = j - 1;
37 break;
38 }
39 }
40 if (isFindEnd == true)
41 {
42 strList.Add(end);
43 rtn.Add(strList);
44 isFindEnd = false;
45 end = string.Empty;
46 }
47 }
48 else
49 {
50 if (isFindEnd == true)
51 {
52 //Add the last
53 if (start == stringList[stringList.Count - 1])
54 {
55 strList.Add(start);
56 rtn.Add(strList);
57 }
58 }
59 else
60 {
61 strList = new List<string>();
62 strList.Add(start);
63 isFindEnd = true;
64 }
65 }
66 }
67
68 return rtn;
69 }

3. Description

This feature is sometimes encountered in the project process. I personally feel that it is a little complicated to write and provide a comment for your discussion.

Contact Us

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

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

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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