A - Takahashi san 2
Problem Statement
KEYENCE has a culture of addressing everyone with the suffix "-san," regardless of roles, age, or positions.
You are given a string SS consisting of lowercase English letters.
If SS ends with san
, print Yes
; otherwise, print No
.
字符串问题 要求判断字符最后三个字母是否为 s a n
令 j = s.length(), 判断 j-3, j-2, j-1即可.
代码如下:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 7e7+10, inf = 2e9;
#define rep(i, a, n) for(int i = a; i <= n; i++)
#define per(i, n, a) for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
int main()
{
IOS
string s;
cin >> s;
int k = s.length();
if(s[k-3] == 's' && s[k-2] == 'a' && s[k-1] =='n' )
cout << "Yes";
else cout << "No";
return 0;
}
B - Unvarnished Report.
Problem Statement
KEYENCE has a culture of reporting things as they are, whether good or bad.
So we want to check whether the reported content is exactly the same as the original text.
You are given two strings SS and TT, consisting of lowercase English letters.
If SS and TT are equal, print 00; otherwise, print the position of the first character where they differ.
Here, if the ii-th character exists in only one of SS and TT, consider that the ii-th characters are different.
More precisely, if SS and TT are not equal, print the smallest integer ii satisfying one of the following conditions:
- 1≤i≤∣S∣1≤i≤∣S∣, 1≤i≤∣T∣1≤i≤∣T∣, and Si≠TiSi=Ti.
- ∣S∣<i≤∣T∣∣S∣<i≤∣T∣.
- ∣T∣<i≤∣S∣∣T∣<i≤∣S∣.
Here, ∣S∣∣S∣ and ∣T∣∣T∣ denote the lengths of SS and TT, respectively, and SiSi and TiTi denote the ii-th characters of SS and TT, respectively.
字符串问题 要求判断两个字符是否一致 如果不一致 输出第一个不一致的字符
代码如下:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 7e7+10, inf = 2e9;
#define rep(i, a, n) for(int i = a; i <= n; i++)
#define per(i, n, a) for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
int main()
{
IOS
string a, b;
cin >> a >> b;
int k = max(a.length(), b.length());
int ok = 0, mark = 0;
rep(i, 0, k-1)
{
if(a[i] == b[i]) continue;
else {
ok = 1;
mark = i;
break;
}
}
if(ok == 0) cout << "0";
else cout << mark+1;
return 0;
}
C - Separated Lunch .
Problem Statement
As KEYENCE headquarters have more and more workers, they decided to divide the departments in the headquarters into two groups and stagger their lunch breaks.
KEYENCE headquarters have NN departments, and the number of people in the ii-th department (1≤i≤N)(1≤i≤N) is KiKi.
When assigning each department to Group AA or Group BB, having each group take lunch breaks at the same time, and ensuring that the lunch break times of Group AA and Group BB do not overlap, find the minimum possible value of the maximum number of people taking a lunch break at the same time.
In other words, find the minimum possible value of the larger of the following: the total number of people in departments assigned to Group AA, and the total number of people in departments assigned to Group BB.
问题;给出多组人数 要求尽可能重新分为两组 使得两组人数之差最小
思路:使用DFS求解 先上代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 2e9;
const ll N = 100000005;
#define rep(i, a, n) for(int i = a; i <= n; i++)
#define per(i, n, a) for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define int long long
int n, ans = inf, cnt, sum, now, a[N];
void dfs(int now, int cnt)
{
if(now > n)
{
ans = min(ans, max(cnt, sum-cnt));
return ;
}
dfs(now+1, cnt);
dfs(now+1, cnt+a[now]);
}
signed main()
{
IOS
cin >> n;
rep(i, 1, n)
{
cin >> a[i];
sum += a[i];
}
dfs(0,0);
cout << ans << endl;
return 0;
}
用数组a存储初始时每一组的人数 用sum表示总人数 dfs中的两个参数分别表示已经遍历过的组和目前拿出的人数(没拿出的自动归入另一组), 即 对于每一组 只有两种可能 拿出或者不拿出
再已经获得的答案与这一轮更新的答案中取最小值即可。
附上另外一道题 来自洛谷
P2392 kkksc03考前临时抱佛脚
内容省略
四个科目 每个科目有n个作业 可同时处理一个科目的两个作业 仍然采用深度优先搜索
四个科目 采用四个for循环遍历 用二维数组存储每个科目与对应的作业量。
dfs()中的两个参数分别为组别和目前搜索到的题目
将左右脑分别取出 类似与分组的思想
对于每个科目下的作业 可以放入左脑 也可以放入右脑
当然也可以回溯
优化的方式是取左右脑max与当前已经获得的Min的最小值
define int long long
#define rep(i, a, n) for(int i = a; i <= n; i++)
#define per(i, n, a) for(int i = n; i >= a; i--)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
int s[5], a[5][25], l, r, minn = inf, ans = 0;
void dfs(int paper, int mark)
{
if(mark > s[paper])
{
minn = min(max(l, r), minn);
return ;
}
l += a[paper][mark];
dfs(paper, mark+1);
l -= a[paper][mark];
r += a[paper][mark];
dfs(paper, mark+1);
r -= a[paper][mark];
}
signed main()
{
IOS
rep(i, 1, 4)
cin >> s[i];
rep(i, 1, 4)
rep(j, 1, s[i])
cin >> a[i][j];
rep(i, 1, 4)
{
l = 0;
r = 0;
dfs(i, 1);
ans += minn;
minn = inf;
}
cout << ans;
return 0;
}
不要忘记void函数的return ;