【今日三题】跳台阶扩展问题(找规律) / 包含不超过两种字符的最长子串 / 字符串的排列(回溯—全排列)

目录
- 跳台阶扩展问题(找规律)
- 包含不超过两种字符的最长子串(字符串哈希)
- 字符串的排列(回溯—全排列)
跳台阶扩展问题(找规律)
- 跳台阶扩展问题
我讨厌找规律题🤡
#include <iostream>
using namespace std;int main()
{int n;cin >> n;cout << (1 << (n - 1)) << endl;return 0;
}
包含不超过两种字符的最长子串(字符串哈希)
- 包含不超过两种字符的最长子串
#include <iostream>
#include <string>
using namespace std;string s;
int len;
int Hash[26];int main()
{cin >> s;for (int l = 0, r = 0, cnt = 0; r < s.size(); r++){if (Hash[s[r] - 'a']++ == 0) cnt++;while (cnt > 2){if (--Hash[s[l++] - 'a'] == 0) cnt--;}len = max(len, r - l + 1);}cout << len << endl;return 0;
}
字符串的排列(回溯—全排列)
- 字符串的排列
class Solution {vector<string> res;string path;bool used[11] = {};
public:vector<string> Permutation(string str) {sort(str.begin(), str.end());dfs(str);return res;}void dfs(const string &str){if (path.size() == str.size()){res.push_back(path);return;}for (int i = 0; i < str.size(); i++){if (!used[i] && (i == 0 || str[i - 1] != str[i] || used[i - 1])){used[i] = true;path += str[i];dfs(str);path.pop_back();used[i] = false;}}}
};
本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~
