维护终点end

1
2
3
4
5
6
7
8
9
bool canJump(int* nums, int numsSize){
int max=numsSize-2,end=numsSize-1;
while(max>=0){
if(nums[max]>=end-max)
end=max;
max-=1;
}
return end?0:1;
}

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
int main(){
long int n, k, res;
scanf("%ld%ld", &n, &k);
res = n;
while(n/k){
res += (n / k);
n = n % k + n / k;
}
printf("%ld", res);
return 0;
}

列表找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>

int fq(int x, int sorx) //sorx==1 a
{
if (x == 1 || x == 2 || (sorx && x == 4) || (!sorx && x == 3))
return 0;
if ((x == 3 && sorx) || (x == 4 && !sorx))
return 1;
int *a = (int *)malloc(sizeof(int) * x);
int *res = (int *)malloc(sizeof(int) * x);
a[0] = a[1] = res[0] = 1;
res[1] = 2;
int j = 2, i;
i = x - 5 + (sorx ? 0 : 1);
while (j <= i)
{
a[j] = a[j - 1] + a[j - 2];
res[j] = a[j] + res[j - 1];
j += 1;
}
return res[i];
}

int main()
{
int a, n, m, x;
scanf("%d%d%d%d", &a, &n, &m, &x);
if (x == 1 || x == 2)
{
printf("%d", a);
return 0;
}
if (x == 3)
{
printf("%d", 2 * a);
return 0;
}
int b = (m - (2 + fq(n - 1, 1)) * a) / fq(n - 1, 0);
printf("%d", (2 + fq(x, 1)) * a + fq(x, 0) * b);
return 0;
}

P1011 [NOIP1998 提高组] 车站

多处用strlen()报TLE😣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char s1[1000010], s2[1000010];
scanf("%s", s1);
scanf("%s", s2);
int len1 = strlen(s1);
int len2 = strlen(s2);
int len = len2;
int *next = (int *)malloc(sizeof(int) * len2);
next[0] = 0;
int x = 1, now = 0;
while (x < len2)
{
if (s2[now] == s2[x])
next[x++] = ++now;
else if (now)
now = next[now - 1];
else
next[x++] = 0;
}
x = now = 0;
while (x < len1)
{
if (s1[x] == s2[now])
{
x++;
now++;
}
else if (now)
now = next[now - 1];
else
x++;
if (now == len2)
{
printf("%d\n", x - now + 1);
now = next[now - 1];
}
}
while (len2--)
{
printf("%d ", next[len - len2 - 1]);
}
return 0;
}

P3375 【模板】KMP字符串匹配

0%