比赛
百度之星初赛第三场
题目链接
题目范围小,可以暴力,然后我是考虑了最优情况,两个攻击高的先打,直到一方死亡,另一方和剩下的打,直到出现第二个死亡
错误代码
反例
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<time.h>
#include<map>
using namespace std;
typedef long long LL;
int a[5];
int main() {
int t;
scanf("%d", &t);
while (t--)
{
memset(a, 0, sizeof(a));
scanf("%d %d %d", &a[0], &a[1], &a[2]);
sort(a, a + 3);
int w = 1;
int b=1000, c=1000,d=1000;
int cnt = 0;
while (b > 0 && c > 0)
{
b -= a[2];
c -= a[1];
cnt++;
}
if (b <= 0 && c <= 0)
{
goto ca;
}
if (b <= 0)
{
while (d > 0 && c > 0)
{
d -= a[2];
c -= a[0];
cnt++;
}
}
else if(c<=0)
{
while (b > 0 && d > 0)
{
d -= a[1];
b -= a[0];
cnt++;
}
}
ca:;
printf("%d\n", cnt);
}
return 0;
}
练习
Count the string
什么??超时?Why???
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int len1, len2;
int next1[200010];
char s1[200010];
char s2[200010];
int ans;
inline void get_next()
{
int t1 = 0, t2;
next1[0] = t2 = -1;
while (t1 < len2)
if (t2 == -1 || s2[t1] == s2[t2])
next1[++t1] = ++t2;
else t2 = next1[t2];
}
inline void KMP()
{
int t1 = 0, t2 = 0;
while (t1 < len1)
{
if (t2 == -1 || s1[t1] == s2[t2])
t1++, t2++;
else t2 = next1[t2];
if (t2 == len2) { ans=(ans%10007)+1; t2 = next1[t2]; }
}
}
int main() {
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &len1);
scanf("%s", s1);
memset(s2, 0, sizeof(s2));
ans = 0;
for (int i = 0; i < len1; i++) {
len2 = strlen(s2);
s2[len2++]= s1[i];
memset(next1, 0, sizeof(next1));
get_next();
KMP();
ans = ans % 10007;
}
cout << ans << endl;
}
return 0;
}
Number Sequence
题目链接
错误思路:一看就是KMP模板题,然后之前写的KMP都是字符串比较的,所以就想着输入每个数加到字符串里就好了
错误代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<time.h>
#include<map>
using namespace std;
typedef long long LL;
string s1, s2;
LL next1[1000010];
LL n, m;
LL w = -1;
inline void get_next()
{
LL t1 = 0, t2;
next1[0] = t2 = -1;
while (t1 < m)
if (t2 == -1 || s2[t1] == s2[t2])
next1[++t1] = ++t2;
else t2 = next1[t2];
}
inline void KMP()
{
LL t1 = 0, t2 = 0;
while (t1 < n)
{
if (t2 == -1 || s1[t1] == s2[t2])
t1++, t2++;
else t2 = next1[t2];
if (t2 == m) {w=t1 - m + 1; t2 = next1[t2]; break; }
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
LL t;
cin >> t;
while (t--)
{
s1.clear();
s2.clear();
cin >> n >> m;
memset(next1, 0, sizeof(next1));
w = -1;
for (LL i = 0; i < n; i++)
{
LL a;
cin >> a;
s1 += a;
}
for (LL i = 0; i < m; i++)
{
LL a;
cin >> a;
s2 += a;
}
get_next();
KMP();
cout << w << endl;
}
return 0;
}
正确思路:字符串的比较是一位一位的比,但是一个数,它可能不是一位,这会导致出错,所以直接用数组KMP就好了
Immediate Decodability
题目链接
一开始是本地运行不对,试了很久,想是不是多组错了,然后越改越错,怀疑初始化方式不对,后来发现是字母敲错了
MLE是数组开大了,题目没说数据范围(委屈)
TLE之后我把cin换成了scanf,然后OLE了(死循环)
最后还是去看了别人的代码,发现其实可以少开一个while
然后终于AC了
2019 蓝桥杯省赛 B 组模拟赛(一)
找质数
错误:超时问题,埃筛优化,溢出问题,剪枝即可
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<map>
#include<stack>
#include<string>
using namespace std;
typedef long long LL;
int prime[2000010];
int main() {
int t;
scanf("%d", &t);
for (int i = 2; i <= 1000000; i++) prime[i] = 1;
for (int i = 2; i <= 1000000; i++) //改成i*i<=1000000
{
if (prime[i])
{
for (int j = i*i; j <= 1000000; j += i) //主要是int的范围导致溢出
{
if (j > 1000000)continue; //这个可以去掉
prime[j] = 0;
}
}
}
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 2; i <= n / 2; i ++)
{
if (prime[i] && prime[n - i])
{
printf("%d %d\n", i, n - i);
break;
}
}
}
}