给出两个字符串S和T,我可以做2个操作。
这两个操作被视为单个步骤。如何找到将S转换为T所需的操作数?
输入: 7 Aaxaabc aabcaax
输出:3
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n;
cin>>n;
char S[n];
char T[n];
cin>>S>>T;
long long deleteCount=0;
long long appendCount=0;
long long ptr=0;
for(long long i=0;i<n;i++){
if(S[i]!=T[i]){
S[i]=S[i+1]; //delete first element
deleteCount++; //count
}
else if(S[i]==T[i] && S[i+1]!='\0'){
if(S[i+1]!=T[i+1]){
S[i]=S[i+1];
deleteCount++;
}
}
if(S[i]==T[i] && S[i+1]=='\0'){
S[i+1]=T[i+1]; //when s is out of element , append S with elements
of T
appendCount++;
}
if(S[i]!=T[i] && S[i+1]=='\0'){
S[i+1]=T[i+1];
appendCount++;
}
}
cout<<S;
cout<<(appendCount+deleteCount)/2;
}
当我打印S时,它不会转换为T但具有其他值。