杭电ACM 1022 感觉是小改动 却出现Runtime Error(ACCESS_VIOLATION) 请各位帮我看看

tang21814 2010-08-03 03:54:00
请问各位 为什么这样写能AC 但是...

#include<iostream>
using namespace std;

int main()
{
int n;
while(cin>>n)
{
if(n>9)
{
cout<<"输入错误,请重新输入"<<endl;
continue;
}
char order1[100],order2[100],stack[100];
cin>>order1>>order2;
int length,i,j;
for(i=0,j=0,length=0;i!=n;)
{
if(length==0||stack[length-1]!=order2[j])//这里...
{
stack[length++]=order1[i++];
}
for(;length>0&&stack[length-1]==order2[j];)
{
--length;
++j;
}
}
if(length==0)
{
cout<<"Yes."<<endl;
for(i=0,j=0,length=0;i!=n;)
{
for(;length==0||stack[length-1]!=order2[j];)
{
stack[length++]=order1[i++];
cout<<"in"<<endl;
}
for(;length>0&&stack[length-1]==order2[j];)
{
--length;
++j;
cout<<"out"<<endl;
}
}
}
else
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
}
return 0;
}


这样写却会出现
Runtime Error
(ACCESS_VIOLATION)呢?
求真相...没什么分了 都赌德国去了....
谢谢大家



#include<iostream>
using namespace std;

int main()
{
int n;
while(cin>>n)
{
if(n>9)
{
cout<<"输入错误,请重新输入"<<endl;
continue;
}
char order1[100],order2[100],stack[100];
cin>>order1>>order2;
int length,i,j;
for(i=0,j=0,length=0;i!=n;)
{
for(;length==0||stack[length-1]!=order2[j];)//和这里... {
stack[length++]=order1[i++];
}
for(;length>0&&stack[length-1]==order2[j];)
{
--length;
++j;
}
}
if(length==0)
{
cout<<"Yes."<<endl;
for(i=0,j=0,length=0;i!=n;)
{
for(;length==0||stack[length-1]!=order2[j];)
{
stack[length++]=order1[i++];
cout<<"in"<<endl;
}
for(;length>0&&stack[length-1]==order2[j];)
{
--length;
++j;
cout<<"out"<<endl;
}
}
}
else
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
}
return 0;
}

...全文
332 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wei121422198 2010-09-02
  • 打赏
  • 举报
回复
到处留下我的足迹。。。。。。。
logiciel 2010-08-09
  • 打赏
  • 举报
回复
在以下循环中length和i没有受到控制

for(;length==0||stack[length-1]!=order2[j];)//和这里... {
stack[length++]=order1[i++];
printf("length=%d i=%d\n", length,i);
}

上面插条printf,用以下输入:
9 135678249 342567891
就可看到length>=100和i>=100了.
tang21814 2010-08-09
  • 打赏
  • 举报
回复
原来如此 谢谢楼上了 明白了!
谢谢!
tang21814 2010-08-08
  • 打赏
  • 举报
回复
依然求解 依然顶一下....
tang21814 2010-08-06
  • 打赏
  • 举报
回复
i!=n i不是在这里有控制吗..?还是没整明白 继续求解...谢谢了
[Quote=引用 4 楼 logiciel 的回复:]
请试以下输入:
9 135678249 342567891

AC的程序在以下语句
if(length==0||stack[length-1]!=order2[j])//这里...
只执行一次。

而出错的程序在以下语句
for(;length==0||stack[length-1]!=order2[j];)//和这里...
可能执行多次,导致length>=100或i>=100……
[/Quote]
logiciel 2010-08-03
  • 打赏
  • 举报
回复
请试以下输入:
9 135678249 342567891

AC的程序在以下语句
if(length==0||stack[length-1]!=order2[j])//这里...
只执行一次。

而出错的程序在以下语句
for(;length==0||stack[length-1]!=order2[j];)//和这里...
可能执行多次,导致length>=100或i>=100,所以出现ACCESS_VIOLATION。
tang21814 2010-08-03
  • 打赏
  • 举报
回复
再求解 再顶一下.....
tang21814 2010-08-03
  • 打赏
  • 举报
回复
求解 顶一下....
tang21814 2010-08-03
  • 打赏
  • 举报
回复
题目帖上来 麻烦大家了

Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3651 Accepted Submission(s): 1326


Problem DescriptionAs the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

InputThe input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
OutputThe output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

Sample Input
3 123 321
3 123 312

Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧