第一题:给一个字符串,其中有不定数量空格相隔的一些单词,计算包含字符 e 的单词个数;
这个比较简单,不列题解了;
第二题,给定四点,计算对应的凸四边形面积,点为给定的结构体;
没写出来:这个基本考数学公式,不知道就做不出来,没有参考价值,不去复盘这个了;
第三题,给定一个字符串,求解字符串子序列中 iflytek 的个数
这个也没写出来,g了,回溯模板都写不对了!!!
backtracking(str, i + 1); 不是backtracking(startIndex + 1);
暴力回溯:
#include <iostream>
using namespace std;
string target = "iflytek";
string path;
int res = 0;
bool isPath(string& path) {
for (int i = 0; i < path.size(); ++i) {
if (path[i] != target[i]) return false;
}
return true;
}
void backtracking(const string& str, int startIndex) {
if (!isPath(path)) return;
if (path == target) {
res++;
return;
}
for (int i = startIndex; i < str.size(); ++i) {
path.push_back(str[i]);
backtracking(str, i + 1);
path.pop_back();
}
}
int findiflytek(string& str) {
backtracking(str, 0);
return res;
}
int main() {
string str = "iflytekiflytek";
cout << findiflytek(str) << endl;
return 0;
}
动态规划解法:待看!