csu 1242 碱基配对

我的思路是先求出类似dp求LCS的数组c,然后计算c的对角线上元素和的最大值,由于和枚举的计算量相同,并且多了给c赋值的过程,耗时要多些,另外也多分配了c这样一个比较大的空间

同样的代码分别用C和C++提交耗时分别为720ms、380ms,内存占用2700kb(C少一些,相差500kb),不解。。所有AC中最快的只有64ms,内存占用740kb。。

对角线边界的判断还是需要点思考的。。

/* csu 1242 */
# include <stdio.h>
# include <string.h>

# define MAXN 1002

char a[MAXN], b[MAXN];
short c[MAXN][MAXN];

short solve(void)
{
short i, j, m, n, s, t, tmp, ans;

memset(c, 0, sizeof(c));

m = strlen(a)-1, n= strlen(b)-1;
for (i = 0; i <= m; ++i)
for (j = 0; j <= n; ++j)
{
tmp = a[i]+b[j];
if (tmp==('A'+'T') || tmp==('C'+'G'))
c[i][j] = 1;
}
s = m, t = 0;
ans = c[i][j];
while (1)
{
tmp = 0;
if (s > 0) --s;
else if (t < n)
++t;
for (i=s, j=t; i<=m && j<=n; ++i, ++j)
tmp += c[i][j];
if (ans < tmp) ans = tmp;
if (s==0 && t==n) return ans;
}
}

int main()
{
while (gets(a) != NULL)
{
gets(b);
printf("%d\n", solve());
}

return 0;
}



原文地址:https://www.cnblogs.com/JMDWQ/p/2387482.html