【编程题】输出字符串中的最大子串

【编程题】输出字符串中的最大子串

题目描述:最大子串的意思就是:所有子串中最大的。大小比较的原则是通过每个字符的比较,比如:b>a,所以b开头的字符串就大于a开头的字符串。

输入:dcbadcbbd
输出:dcbbd

输入:abdasd
输出:sd

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     string s;
 8     cin >> s;//字符串不能有空格,否则要换种输入方式
 9     int len = s.size();
10     int head=0;
11     for (int i = 0; i < len; i++)
12     {
13         if (s[i] > s[head])
14             head = i;
15         else if (s[i] == s[head])
16         {
17             int p= head + 1;
18             int q = i + 1;
19             while(s[p]==s[q]&&q<len)
20             {
21                 p++;
22                 q++;
23             }
24             if (q != len&&s[q] > s[p])
25                 head = i;
26         }
27         else
28             ;
29     }
30     //输出方法一
31     string ss(s, head);
32     cout << ss << endl;
33     //输出方法二
34     string sss(s, head, len - head);
35     cout << sss<<endl;
36     //输出方法三
37     for (int i = head; i < len; i++)
38         cout << s[i];
39     cout << endl;
40     
41     system("pause");
42     return 0;
43 }
原文地址:https://www.cnblogs.com/engraver-lxw/p/7641624.html