TYVJ 1113 魔族密码 解题报告
DP:f[i] = f[j] + 1 (1<= j < i, 且j是i的前缀.)
代码如下:
#include <stdio.h> #include <string.h> #include <stdlib.h> char str[2001][501]; int f[2001]; int ans = 0; int main(int argc, char **argv) { int i, j; int n; scanf("%d", &n); for(i = 1; i <= n; i++){ scanf("%s", str[i]); } for(i = 1; i <= n; i++){ for(j = 1; j < i; j++){ if(f[j] > f[i] && strncmp(str[i], str[j], strlen(str[j])) == 0){ f[i] = f[j]; } } f[i]++; if(ans < f[i]){ ans = f[i]; } } printf("%d\n", ans); return 0; }