B3812 std::string 题解

· · 题解

B3812 std::string 题解

Hack 的方法:

$2:$ 炸数组(越界)! $3:$ 超时! 要领: 看数据范围! ## 进入正题: ### Problem $1$: 你需要 Hack 的 Code: ```cpp #include <iostream> using namespace std; int main() { int n; string ans, base = "HelloLuogu"; cin >> n; while (n--) ans = ans + base; cout << ans << endl; } ``` ~~(属于用不正常的方法做一个入门题)~~ 非常的明显,当 $n$ 极大的时候,此程序会超时(炸掉),在本地 Dev-C++ 一试,$10$ 秒了,还是没有响应,所以 Problem $1$ 的代码大家应该知道了吧~~~ ### Problem $2$: 你需要 Hack 的 Code: ```cpp #include <vector> #include <iostream> #include <algorithm> int T; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> a(n + 1), pre(n + 2), post(n + 2); for (int i = 1; i <= n; ++i) std::cin >> a[i]; pre[1] = a[1]; for (int i = 2; i <= n; ++i) pre[i] = pre[i - 1] & a[i]; post[n] = a[n]; for (int i = n - 1; i; --i) post[i] = post[i + 1] & a[i]; int ans = 1; for (int i = 1; i < n; ++i) if (pre[i] + post[i + 1] == pre[n]) { ++ans; } std::cout << ans << '\n'; } ``` ~~(稍微有点难)~~ Problem $2$ 想要炸数组是不可能滴,因为有 vector,炸不了啊~~~ 但我们发现第二个循环,是从 $2$ $∼$ $n$ 的,所以只要输入一个 $0$ 程序就会 WA 掉(即 Wrong Answer) ### 代码 ~~(你们最喜欢的部分来喽~)~~ ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int x; cin>>x; if(x==1) cout<<100000; else if(x==2){ cout<<6<<endl; cout<<114514<<" "<<0<<" "<<22332<<" "<<1000000000<<" "<<98898<<" "<<65536; }else{//this code will not be executed cout<<"I AK IOI!!!!!!"; } return 0; } ```