dp【j-1】这里当j==0时为什么不报错??
#include
#include
#include
using namespace std;
char str[1010];
int dp[1010];
bool judge(int x,int y) //判断是不是回文串
{
while(x <= y)
{
if(str[x] != str[y])
return false;
x++;
y--;
}
return true;
}
int main()
{
int len, i, j;
while(gets(str) != NULL)
{
len = strlen(str);
for(i = 0; i < len; i++)
{
dp[i] = i + 1; //假设前面的都不能组成回文串
for(j = 0; j <= i; j++)
if(str[j] == str[i] && judge(j,i))
dp[i] = min(dp[i], dp[j-1]+1);
}
printf("%d\n",dp[len-1]);
}
return 0;
}

下面的代码数组不是越界了吗??为什么还可以运行而且不报错??
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
6条回答 默认 最新
- LemonSmile_ 2015-05-14 09:09关注
C++语言中数组越界访问系统不会给出任何的提示,程序员可以超出数组边界进行读/写从而造成内存的混乱,而这种错误对初学者来说是很容易出现的、而又偏偏是很难调试的,因为系统不会给出错误的提示,所以就这样使用数组是不安全的。
http://blog.sina.com.cn/s/blog_60e96a410100lpqt.html本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报