一个字符串中所有子的长度+ sort逆序+输入不定量的字符串数组

对于指定的一个非空字符串数组,由若干个子串组成,查找并返回此数组中长度第N长的字符串长度,如果不存在,则返回字符串中长度最长子串的长度
输入:第一行是字符串数组(不定数量)
第二行是整数N
输出:一个整数,值为该子串的长度
input: abc ab ab
3
output: 3
input: abc ab a
2
output:2
#include
#include
#include
#include
#include
#include
using namespace std;
void lengthOfWord(string s,vector &res) { //每个字符串的长度
istringstream is(s);
int sum = 0;
string temp;
while (is >> s) {
temp = s;
sum = temp.size();
res.push_back(sum);
}
}
bool cmp(int a, int b) { //从大到小排序
return a > b;
}
int main() { ///输入不定数量的字符串数组
string a ;
vector b;
string c;
cin >> a;
b.push_back(a);
while (cin.get() != ‘\n’) {
cin >> a;
b.push_back(a);
}
for (int i = 0; i < b.size(); i++) {
c += b[i]+ " ";
}
int n;
cin >> n;
vector res;
lengthOfWord(c, res);
sort(res.begin(), res.end(),cmp);
res.erase(unique(res.begin(), res.end()), res.end());
vector::iterator iter = res.begin();
if (n > res.size()) {
cout << *(iter) << endl;
} else {
cout << *(iter + n - 1);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值