第一题没啥好说的,简单题
inline bool judge(char c)
{
return c=='a'||c=='e'||c=='i'||c=='o'||c=='u';
}
bool isBaiduStr(string &s, int i)
{
unordered_set<char> se{s[i], s[i+1], s[i+2], s[i+3], s[i+4]};
return se.size()==5 && !judge(s[i])&&judge(s[i+1])&&judge(s[i+2])&&!judge(s[i+3])&&judge(s[i+4]);
}
int main()
{
string s;
cin>>s;
int ans=0;
for (int i = 0; i + 4 < s.size(); ++i)
{
ans+= isBaiduStr(s,i)?1:0;
}
cout<<ans;
return 0;
}
第二题我的思路是判断是否能变成全0或全1,相当于一个长度为2从左往右的滑窗,离开滑窗的位置不会再有修改的机会,所以当滑窗左边这个值跟目标值不一致时就需要做一次翻转
#include <bits/stdc++.h>
using namespace std;
bool judge(vector<int> v, int x)
{
for (int i = 1; i < v.size(); ++i)
{
if(v[i-1]!=x)
{
v[i-1]=!v[i-1];
v[i]=!v[i];
}
}
int sum=0;
for (auto &num:v)
sum+=num;
return sum==x*v.size();
}
int main()
{
string s;
int t;
cin>>t;
while (t--)
{
cin>>s;
vector<int> v;
for (auto &c:s)
v.push_back(c-'0');
cout<<(judge(v, 0) || judge(v, 1)?"Yes":"No")<<endl;
}
return 0;
}
第三题也没啥好说的,BFS就完事
#include <bits/stdc++.h>
using namespace std;
int dx[]{0, 0, 1, -1}, dy[]{1, -1, 0, 0};
int n, m;
typedef pair<int, int> pii;
struct pairHash
{
size_t operator()(const pair<int, int> &p) const
{
return p.first * m + p.second;
}
};
inline bool movable(char a, char b)
{
return !(a == 'r' && b == 'd' || a == 'e' && b == 'r' || a == 'd' && b == 'e');
}
int BFS(const vector<string> &g)
{
queue<pii> que;
que.push(make_pair(0, 0));
vector<vector<bool>> vis(n, vector<bool>(m, false));
vis[0][0] = true;
unordered_map<pii, int, pairHash> levs;
levs[make_pair(0, 0)] = 0;
while (!que.empty())
{
auto [x, y] = que.front();
que.pop();
int curLev = levs[make_pair(x, y)];
if (x == n - 1 && y == m - 1)
return curLev;
for (int i = 0; i < 4; ++i)
{
int newX = x + dx[i], newY = y + dy[i];
if (newX >= 0 && newX < n && newY >= 0 && newY < m && movable(g[x][y], g[newX][newY]) && !vis[newX][newY])
{
que.push(make_pair(newX, newY));
levs.insert(make_pair(make_pair(newX, newY), curLev + 1));
vis[newX][newY] = true;
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
vector<string> g(n);
for (int i = 0; i < n; i++)
cin >> g[i];
cout << BFS(g);
return 0;
}
#百度笔试#