多个查询的循环过早终止
Loop terminating prematurely for multiple queries
对于这个问题,我需要执行多个查询,所以我为它使用了 for 循环
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>ans; //vector to store the ans
string s,e; //real dna and virus dna
cin>>s;
int q,start,match=0; // no. of queries, starting value of the query, var to store matching occurence
unsigned int step=0; //var to keep count of virus dna iteration
cin>>q;
for(int i=0;i<q;i++){//loop to iterate q times
cin>>start;
int l,r,x,c;
if(start==2){ // for 2nd type query
cin>>l>>r>>e;
for(int i=l-1;i<=r-1;i++){
if(s[i]==e[step]){
match+=1;
}
step+=1;
if(step== e.length()){
step=0; //starting again from start of virus
}
}
}
ans.push_back(match);
match=0; //reintializing value for next query
if(start==1){ //for 1st type query
cin>>x>>c;
s[x-1]=c; //replacing char at x-1 with c
}
}
for(int j=0;j<ans.size();j++){ //loop for ans output
cout<<ans[j]<<endl;
}
return 0;
}
但它在应该之前终止,例如:对于此输入,
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
它将在第 5 行停止并打印 8,2,0, 0,而它应该是 8、2、4。如果我在没有循环的情况下进行单个查询,事情可以正常工作,但任何类型的循环都不起作用。请帮忙。此外,任何更有效地解决此类问题的建议都将对我非常有帮助。
代码中更大的问题是变量c
被定义为int
int l,r,x,c;
当接收字符而不是整数(在您的示例中为T
(时,应定义为char
int l,r,x;
char c;
如果c
int
,当您将4 T
发送到
cin>>x>>c;
x
接收 4,c
没有收到T
(T
不是有效的int
(,因此开始外部循环的下一次迭代,称为
cin>>start;
当T
留在缓冲区中时;start
是整数,所以有同样的问题,程序结束。
而且,正如很快指出的那样,ans.push_back()
应该在第一个如果之内。现在,还添加了一个值(零(,并以1
开头的行。
相关文章:
- while 循环不以哨兵值终止
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- C++ 即使不满足条件,循环也会终止
- 为什么 EOF 在循环扫描期间没有终止?
- 对于检查 >=0 终止条件时的循环索引类型
- 程序在终止循环后不接收输入
- 即使我尝试递减 t,While 循环也没有终止
- 如何修复不会在退出选择时终止的 do-while 循环
- 如何用字符终止整数输入循环?
- 读入变量时循环终止,'Range_error' on C++
- 当我输入非整数值时,我的二次方程代码终止,如何让它正确循环?
- 如何正确终止此循环?
- 多个查询的循环过早终止
- 程序终止,状态为 -1073741510 在 for 循环的中间,将输入分配给二维数组
- 终止具有特定字符的循环
- 程序终止,而不是继续整数输入验证循环
- 程序似乎在 for 循环 - C++后静默终止
- 循环终止不正确时
- C :终止循环的最优雅方法,该循环在字符串向量上迭代
- 如果未检测到alpha,则终止循环