蔚来技术笔试AC

三道算法
1,2简单题
3、两个字符数S,T,每次可以将S中的一个移动到末尾,最少移动多少次使S==T
#include
#include
#include
using namespace std;

int main() {
string S,T;
cin >> S >> T;
if(S==T)return 0;
int len = S.length();
int result =len;
map S_map{{'a',0},{'b',0},{'c',0},{'d',0},{'e',0},{'f',0},{'g',0},{'h',0},{'i',0},{'j',0},{'k',0},{'l',0},{'m',0},{'n',0},{'o',0},{'p',0},{'q',0},{'r',0},{'s',0},{'t',0},{'u',0},{'v',0},{'w',0},{'x',0},{'y',0},{'z',0}};
map T_map{{'a',0},{'b',0},{'c',0},{'d',0},{'e',0},{'f',0},{'g',0},{'h',0},{'i',0},{'j',0},{'k',0},{'l',0},{'m',0},{'n',0},{'o',0},{'p',0},{'q',0},{'r',0},{'s',0},{'t',0},{'u',0},{'v',0},{'w',0},{'x',0},{'y',0},{'z',0}};
for(int i=0;iS_map[S[i]]+=1;
T_map[T[i]]+=1;
}
for(map::iterator itr=S_map.begin();itr!=S_map.end();itr++){
if(T_map[itr->first]!=itr->second)return -1;
}
for(int i=0;iint local=0;
local = S.find(T[i]);
if(local ==-1)break;
else{
S.erase(S.begin(),S.begin()+local+1);
result-=1;
}
}
cout<}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务