Submenu longest string of repeated characters ⽆

⼀ given string, you find out which does not contain a repeat length of the longest string of characters Submenu.
Example 1 Indicator:
Input: "abcabcbb"
Output: 3
Explanation: Because the longest substring of characters without repetition is "abc", so its length is 3.
Example 2 Indicator:
Input: "bbbbb"
Output: 1
Explanation: Because the longest substring is repeated characters without "b", so that its length is 1.
Indicator Example 3:
Input: "pwwkew"
Output: 3
Explanation: Because the longest sub-string is repeated characters without "wke", so its length is 3.
Please note that your answer must be a substring of length, "pwke" is a sub-sequence, not a substring.

Problem-solving ideas:

Create an array, and is initialized to 0, a total of 256 elements in the array, the array subscript represents the ASCII code characters. Traversal string from the beginning, when there is a character, the character in the array element where the recording standard offset location of the character (relative to the starting position) in the string. Traversing a string of characters found in the character array element index value is 0, it means that the character has not been seen, and this represents a left looking for the longest substring starting position where the string (character does not represent where the longest substring string starting position). When traversing a character string found in the character array element index value is 0 when the value of the left or less than, the length of the non-repeatable character is max (maximum length value has been saved, the current offset value of traversing a character -left + 1).

 1 #include<string>
 2 #include<iostream>
 3 using namespace std;
 4 int lengthOfLongestSubstring(string s)
 5 {
 6     int m[256] = {0}, res = 0, left = 0;
 7     for (int i = 0; i < s.size(); ++i)
 8     {
 9         if (m[s[i]] == 0 || m[s[i]] < left)
10         {
11             res = max(res, i - left + 1);
12         } 
13         else
14         {
15             left = m[s[i]];
16         }
17             m[s[i]] = i + 1;
18     }
19      return res; 
20 }
21 int main()
22 {
23     string mystring;
24     while(1)
25     {
26         cin>>mystring;
27         if(mystring.compare ( " Exit " ) == 0 )
 28          {
 29              BREAK ;
 30          }
 31 is          COUT << " maximum length without repeated characters: " << lengthOfLongestSubstring (MyString) << endl;           
 32      }
 33 is      return  0 ;
 34 is }

If the program statement why there must be a condition m [s [i]] <left, we use an example to illustrate, if the input string is "abbca" time, when i = 4, which is about to be traversing the start of a last letter, the array element values ​​at this time where a subscript character corresponding to 1, b corresponding to 3, c corresponding to 4, left 2, i.e., the left boundary of the current longest substring of a second b position, and has a first not within the scope of the current longest string, then for i = 4 of a new incoming, the results should be added, but this time is not updated hash table a is 1, not 0, and if it is not determined relationship, then the left, the results can not be updated, then the answer will be a little, it is necessary to add m [s [i]] <left.

Guess you like

Origin www.cnblogs.com/jest549/p/11466462.html