蓝桥杯A组 字串排序 C++
时间: 2023-09-28 21:08:38 浏览: 71
题目描述
输入n个字符串,按照字典序从小到大排序后输出。
输入格式
第一行包含一个整数n。
接下来n行,每行包含一个字符串。
输出格式
共n行,按照字典序从小到大排序后输出。
数据范围
1≤n≤105,
每个字符串的长度不超过100。
输入样例
3
nowcoder
hello
world
输出样例
hello
nowcoder
world
算法1
(暴力枚举) $O(n^2)$
1.暴力枚举
时间复杂度
参考文献
python3 代码
C++ 代码
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
相关问题
c++判断字符串是否为字串
以下是C++中判断字符串是否为子串的方法:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "hello world";
string str2 = "world";
if (str1.find(str2) != string::npos) {
cout << "str2是str1的子串" << endl;
} else {
cout << "str2不是str1的子串" << endl;
}
return 0;
}
```
输出结果为:`str2是str1的子串`。在上述代码中,我们使用了`find()`函数来查找子串。如果`find()`函数返回的结果不是`string::npos`,则说明子串存在于原字符串中,否则不存在。
C++ 判断字符串包含某个字串
在 C++ 中,你可以使用字符串的 find 函数来判断一个字符串是否包含某个子串。下面是一个示例代码:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
std::string substr = "World";
size_t found = str.find(substr);
if (found != std::string::npos) {
std::cout << "字符串包含子串" << std::endl;
} else {
std::cout << "字符串不包含子串" << std::endl;
}
return 0;
}
```
在上述代码中,我们使用 `find` 函数在字符串 `str` 中查找子串 `substr`。`find` 函数返回子串在字符串中的位置,如果找不到则返回 `std::string::npos`。通过判断 `found` 是否等于 `std::string::npos`,我们可以确定字符串是否包含子串。
希望对你有帮助!如果还有其他问题,请随时提问。