版权声明:本人ZZU在校学生,文章均为个人心得,有不足之处请不吝赐教! https://blog.csdn.net/whl_program/article/details/82599325
思路:
1. 字符串按长度在vector二维数组中存储,排序
2. 按行排序
3. 递归输出
#include<bits/stdc++.h>
using namespace std;
vector<string> res;
int cmp(string a,string b){
return a<b;
}
vector<string> result[100];
void print(int strLen,int pos){
if(result[strLen].size() != 0){
int len = result[strLen].size();
for(int i=0; i<len; i++){
string stemp = result[strLen][i].substr(0,strLen-2);
if(result[strLen-2][pos] == stemp){//判断是否是子串
for(int j=1; j<strLen/2; j++)//根据长度输出制表符
cout << " ";
cout << result[strLen][i] << endl;
print(strLen+2, i);//递归输出子串
}
}
}
}
int main(){
string s;
int n = 0;
while(cin >> s){
res.push_back(s);
n++;
}
for(int i=0;i<n;i++){
int k = res[i].length();
if(k%2 != 0)
continue;
result[k].push_back(res[i]);
}
for(int i=2;;i+=2){
if(result[i].size()==0)
break;
sort(result[i].begin(),result[i].end(),cmp);
}
int len = result[2].size();
for(int i=0; i<len; i++){
cout << result[2][i] << endl;
print(4, i);
}
}
//01 0101 0301 03 030101 0201 02 0303 0401 033 03010101
思路:
1. 分割字符串 存储
2. 求最大连续子序列问题
3. 定义变量保存局部值,详见代码
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
void foo(vector<int> &arr){
if(arr.size() == 0)
return;
int startNum = arr[0];
int maxCount = 1;
int tempStart = arr[0];//当前连续序列首个数字
int tempCount = 1;
for(int i=1; i<arr.size(); i++){
if(arr[i]-arr[i-1] == 1){
tempCount++;
}else{
if(tempCount > maxCount){
maxCount = tempCount;
startNum = tempStart;
}
tempStart = arr[i];
tempCount = 1;
}
}
//好坑啊 逗号后有一个空格 浪费好长时间检查错误
cout << "[" << startNum << ", " << maxCount << "]" << endl;
}
int main()
{
vector<int> arr;
string str;
getline(cin, str);
if(str == ""){
return 0;
}
int x1 = 0;
int x2 = 0;
int int_temp;
string stemp;
while (x2 != -1){
x2 = str.find(0x20, x1);//返回首次匹配的空格的下标 0x20是空格的ASCII码
stemp = str.substr(x1, x2 - x1);//截取从字符串str中第x1位开始的长度为(x2-x1)的字符串
int_temp = atoi(stemp.c_str());
arr.push_back(int_temp);
x1 = x2 + 1;//更改下次查询起始位置
}
stemp = str.substr(x1, str.size()-1-x1);
int_temp = atoi(stemp.c_str());
arr.push_back(int_temp);
cout << endl;
foo(arr);
return 0;
}
//1 4 5 6 7 8 10 11 12 14 18 -1