我在C ++中编写了一段代码,它尝试更改字符串1中的某些字符,如果与字符串2匹配并且在指定的更改次数中,则会打印YES,否则为NO。 这是程序:
#include <iostream>
#include <cstring>
class DaisyTransformation
{
private:
int moves,status,length;
char S1[50],S2[50],S3[50];
public:
void getValues( )
{
std::cin >> S1 >> S2 >> moves;
length=strlen(S1);
}
void printValues( )
{
//std::cout <<"After"<< S1<<S2;
if(status==0)
std::cout << "No\n";
else
std::cout << "Yes\n";
}
void printValues1( )
{
std::cout <<"After"<< S1<<S2;
}
void calcPossibilities()
{
int i=1,j=0;
//std::cout << "Before swapping" << S1<<S2;
for(i=0;i<=moves&&j<length;){
if(S1[j]==S2[j]){
//S3[j]=S1[j];
j++;
//std::cout <<"\ncond"<< 1<<j;
}
else{
if(S1[j]=='M'){
S1[j]='D';
i++;
j++;
//std::cout <<"\ncond1"<<i<<j ;
}
else{
S1[j]='M';
i++;
j++;
//std::cout << "\ncond2"<<i<<j;
}
}
}
if(strcmp(S1,S2)==0 && i==moves)
status=1;
else
status=0;
}
};
int main()
{
DaisyTransformation dt[250];
int noOfTestCases,i;
std::cin>>noOfTestCases;
for(i=0;i<noOfTestCases;i++){
std::cin.clear();
dt[i].getValues();
}
for(i=0;i<noOfTestCases;i++){
/*if(i==24)
dt[i].printValues1();*/
dt[i].calcPossibilities();
/*if(i==24)
dt[i].printValues1();*/
dt[i].printValues();
}
return 0;
}
当我的测试用例较少时(例如20次),此代码运行完美。但是,当我为更多测试用例(比如说30以上)执行相同的程序时,程序返回错误的输出。
示例输入 - 执行正常
4
MMMMMDMDMDMMDMDMDM DDDDDDDDDDDDDDDDDD 12
MMMMMDMDMDMMDMDMDM DDDDDDDDDDDDDDDDDD 12
MMMMMDMDMDMMDMDMDM DDDDDDDDDDDDDDDDDD 12
MMMMMDMDMDMMDMDMDM DDDDDDDDDDDDDDDDDD 12
输出:
Yes
Yes
Yes
示例输入 - 执行不正确
30
MMMMMDMDMDMMDMDMDM DDDDDDDDDDDDDDDDDD 12
MMM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MDMDM DDDDDDDDDDDDDDDDDD 12
MMMMMDMDMDMMDMDMDM DDDDDDDDDDDDDDDDDD 12
MMMMMDMDMDMMDMDMDM DDDDDDDDDDDDDDDDDD 12
输出
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
**No**
Yes
Yes
Yes
Yes
Yes
我仍然无法找到执行第24个测试用例时返回 NO 的原因,当它应该返回是
正在使用的编译器是g ++ 4.8.4