Hello 2019题解
2024-09-21 18:35:29
Hello 2019题解
题解 CF1097A 【Gennady and a Card Game】
map大法好qwq
枚举每一个的第\(1,2\)位判是否与给定的重复即可
# include <bits/stdc++.h>
std::map<char, int> m1, m2;
int main()
{
std::string s[6], str;
std::cin >> str;
for(int i = 1; i <= 5; i++)
std::cin >> s[i], m1[s[i][0]] = 1, m2[s[i][1]] = 1;
if(m1[str[0]] || m2[str[1]])
return printf("YES\n") * 0;
printf("NO\n");
return 0;
}
题解 CF1097B 【Petr and a Combination Lock】
\(dfs\)入门题,下一个
枚举每一个是正还是负,最后判能不能被\(360\)整除即可
时间复杂度\(O(2^n)\)
# include <bits/stdc++.h>
int n, flag, a[20], used[20];
inline void check()
{
int sum = 0;
for(int i = 1; i <= n; i++)
sum += a[i] * (used[i] ? (-1) : 1);
if(sum % 360 == 0)
flag = 1;
}
void dfs(int x)
{
if(x == n + 1)
return (void) (check());
dfs(x + 1);
used[x] = 1;
dfs(x + 1);
used[x] = 0;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
dfs(1);
printf(flag ? "YES" : "NO");
return 0;
}
题解 CF1097C 【Yuhao and a Parenthesis】
说起Yuhao,我就不得不提@BlueCat,想起了他旋转**的情景,明年,中美,两开花,关注
对于每个括号序列,我们都用栈预处理出它不能配对的部分
等等?什么叫不能配对的部分?
比如说序列\()())\),\())\)就是它不能配对的部分
然后把这个部分取反
就得到了如果要把这个序列弄成正确的需要的最简序列\(x_i\)
显然这个部分要放在当前串的前面或后面
但是我们有些时候发现这东西是假的
比如这个序列\())))((((\),它的匹配序列是\((((())))\),很明显放前或后都不行,那么这个串就废了
如果这个串没废,就把\(x_i\)的计数累加\(1\)
然后我们枚举每一个\(x_i\),将\(ans\)累加上\(\min{x_i,x_i取反}\)(取反只把一个括号序列中'('变成')',反之亦然)
注意这里有一个特判,如果有一个串本来就是好的,那么统计答案时,由于空串的反串还是空串,所以答案会算两次
所以要特判出好串的个数\(cnt\)
最后输出\(ans/2+cnt/2\)即可(下取整)
#include <bits/stdc++.h>
const int MaxN = 100010, MaxM = 500010;
std::string s[MaxN], str[MaxN];
std::map<std::string, int> m;
inline std::string change(std::string s)
{
int len = s.length();
std::string tmp = "";
for (int i = 0; i < len; i++)
tmp += ((s[i] == '(') ? ')' : '(');
return tmp;
}
inline int check1(std::string s)
{
std::stack<char> st;
int len = s.length();
for (int i = 0; i < len; i++)
{
if (s[i] == '(')
st.push('(');
else if (s[i] == ')' && st.empty())
return 0;
else
st.pop();
}
return 1;
}
inline void check(int x, std::string s)
{
std::vector<char> st;
int len = s.length();
for (int i = 0; i < len; i++)
{
if (st.empty())
{
st.push_back(s[i]);
continue;
}
if (s[i] == '(')
st.push_back('(');
else if (s[i] == ')' && st.back() == '(')
st.pop_back();
else if (s[i] == ')' && st.back() == ')')
st.push_back(')');
}
std::string tmp = "";
for (int i = 0; i < st.size(); i++)
tmp += ((st[i] == '(') ? ')' : '(');
if (check1(tmp + s) || check1(s + tmp))
++m[tmp];
// std::cout << " " << s << " " << tmp << "\n";
}
int main()
{
int n, ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
std::cin >> s[i], check(i, s[i]);
for (std::map<std::string, int>::iterator it = m.begin(); it != m.end(); ++it)
{
if (it->first == "")
continue;
ans += std::min(it->second, m[change(it->first)]);
}
printf("%d\n", (ans / 2) + (m[""] / 2));
return 0;
}
最新文章
- SQL执行效率2-执行计划
- PDO和消息队列的一点个人理解
- 使用tomcat部署jsp程序
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
- I/O复用模型之select学习
- CMD和DOS的区别
- Webstorm的序列号和证书
- Android Studio 第一次新建Android Gradle项目超级慢的解决方案
- jquery+php实现用户输入搜索内容时自动提示
- c#知识总结1
- 【Construct Binary Tree from Inorder and Postorder Traversal】cpp
- Linux常用命令之grep
- Android手机监控软件设计实现
- mysql 安装截图
- 解决Ubuntu系统中文乱码显示问题,终端打开文件及查看目录
- Arduino单片机使用和开发问题记录(转)
- java操作redis redis连接池
- npm killed有可能是内存不够, 为Ubuntu增加swap
- python笔记06-10
- centos7-网络连接