51nod1732 51nod婚姻介绍所(DP)
程序员文章站
2022-05-11 14:13:45
...
原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1732
思路:简单的dp,预处理字符串的自匹配的就好,if str[i]==str[j] 有 dp[i][j] = 1+dp[i+1][j+1],用输入输出挂加速读入写出
AC代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
const int MAXN = 1e3 + 5;
const int MOD = 1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
const double E = exp(1.0);
using namespace std;
inline void readInt(int &x) {
char c;
x = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0'&&c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
}
int dp[MAXN][MAXN], buf[10];
inline void writeInt(int i) {
int p = 0;
if (i == 0)p++;
memset(buf, 0, sizeof(buf));
if (i == 0)p++;
else {
while (i) {
buf[p++] = i % 10;
i /= 10;
}
}
for (int j = p - 1; j >= 0; j--)putchar('0' + buf[j]);
}
int main() {
int n, q;
char str[MAXN];
scanf("%d", &n);
scanf("%s", str);
int len = strlen(str);
for (int i = len - 1; i >= 0; i--) {
for (int j = i; j >= 0; j--) {
if (i == len - 1 || j == len - 1) {
dp[i][j] = (str[i] == str[j]);
}
else {
if (str[i] == str[j]) {
dp[i][j] = dp[i + 1][j + 1] + 1;
}
}
}
}
scanf("%d", &q);
while (q--) {
int a, b;
readInt(a);
readInt(b);
if (a < b) {
a = a + b;
b = a - b;
a = a - b;
}
printf("%d\n", dp[a][b]);
}
return 0;
}
上一篇: Docker的volumes踩坑
下一篇: 51Nod - 1174 RMQ算法