就那几种 成三 的情况,日常暴力,秒天秒地(我室友常这样说)。这题不用动脑子,看着图随便写。
我把图的横纵左右都+3了
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 17;
int pic[maxn][maxn];
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)) {
int x, y;
memset(pic, 0, sizeof(int) * maxn * maxn);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &x, &y);
pic[x + 3][y + 3] = 1;
}
for (int i = 0; i < m; ++i) {
scanf("%d%d", &x, &y);
}
int cnt = 0;
for (int i = 2; i <= 4; ++i) { //2-4行 ,
for (int j = 0; j <= 6; ++j) {
if (pic[i][j] && pic[i][j + 1] && pic[i][j + 2]) {
cnt++;
}//竖着看
}
}
for (int i = 2; i <= 4; ++i) { //2-4列
for (int j = 0; j <= 6; ++j) {
if (pic[j][i] && pic[j + 1][i] && pic[j + 2][i]) {
cnt++;
} //横着看
}
}
int k = 2, r = 1, c = 1; //竖着看
if (pic[r][c] && pic[r][c + k] && pic[r][c + 2*k]) {
cnt++;
}
r = 5;
if (pic[r][c] && pic[r][c + k] && pic[r][c + 2*k]) {
cnt++;
}
r = 0, k = 3, c = 0;
if (pic[r][c] && pic[r][c + k] && pic[r][c + 2*k]) {
cnt++;
}
r = 6;
if (pic[r][c] && pic[r][c + k] && pic[r][c + 2*k]) {
cnt++;
}
// ===========
k = 2, c = 1, r = 1; //横着看
if (pic[r][c] && pic[r + k][c] && pic[r + 2*k][c]) {
cnt++;
}
c = 5;
if (pic[r][c] && pic[r + k][c] && pic[r + 2*k][c]) {
cnt++;
}
c = 0, k = 3, r = 0;
if (pic[r][c] && pic[r + k][c] && pic[r + 2*k][c]) {
cnt++;
}
c = 6;
if (pic[r][c] && pic[r + k][c] && pic[r + 2*k][c]) {
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
KMP算法的应用,刚开始我用STL中的string,用string::find()超时了两次,果断KMP,哈
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 1e6 + 7;
int n, a, b, L, R, cur;
int Next[maxn];
char s[maxn], t[maxn];
void preKmp(char T[], int m, int Next[]) {
int i = 0, j = -1;
Next[0] = -1;
while (i<m) {
while (j != -1 && T[i] != T[j]) j = Next[j];
if (T[++i] == T[++j]) Next[i] = Next[j];
else Next[i] = j;
}
}
int KMP_Count(char T[], int m, char S[], int n) {
//T是模式串,S是主串
int i = 0, j = 0;
int ans = 0;
preKmp(T, m, Next);
while (i<n) {
while (j != -1 && S[i] != T[j])
j = Next[j];
++i; ++j;
if (j >= m) {
ans++;
j = Next[j];
}
}
return ans;
}
void fun(char str[], int n) {
if (L <= n && n <= R) {
if (n & 1) {
str[cur++] = 'T';
}
else {
str[cur++] = 'A';
}
}
else {
if (n & 1) {
str[cur++] = 'C';
}
else {
str[cur++] = 'G';
}
}
}
int main()
{
while (~scanf("%d%d%d%d%d", &n, &a, &b, &L, &R)) {
scanf("%s", t);//不要改为gets(t);改之后,会发现莫名输出一个n的值,很奇怪的编译器的bug
int lenT = strlen(t);
int w0 = b, w1;
cur = 0;
fun(s, w0);
for (int i = 1; i < n; ++i) {
w1 = (w0 + a) % n;
fun(s, w1);
w0 = w1;
}
s[n] = '\0';
//puts(s);
printf("%d\n", KMP_Count(t, lenT, s, n));
}
return 0;
}
\
\
\
\