阿里笔试第一题
在S中查找T的序列,对于当前S[i],如果不等于T[j],那么让i+1,最后判断下-1的情况就ok了
/// file :: 2jjalloc.h #ifndef _JJALLOC_ #define _JJALLOC_ #include<new>/// for placement new #include<cstddef>/// for ptrdiff_t,size_t; #include<cstdlib>/// for exit() #include<climits>/// for unit_max #include<iostream>///for cerr #endif namespace JJ { template <class T> inline T* _allocate(ptrdiff_t siz,T *) { std::set_new_handler(0); T* tmp = (T*)(::operator new ((size_t)(siz*(sizeof(T))))); if(!tmp){ std::cerr<<"out of merry"<<std::endl; exit(1); } return tmp; } template <class T> inline void _delallocate(T*buffer){ ::operator delete(buffer); } template <class T> inline void _destory(T* tmp){ tmp->~T(); } template <class T1,class T2> inline void _construct(T1 *p,const T2& val){ new (p) T1(val); } } #include<bits/stdc++.h> using namespace std; map<char,int>mp; int main() { string str1,str2; cin>>str1>>str2; int i=0,j=0; for(i=0;i<str1.size();++i){ if(str1[i]==str2[j])++j; else mp[str1[i]]++; } int ans=str1.size()-j; for(j;j<str1.size();++j){ mp[str2[j]]--; if(mp[str2[j]]<0)ans=-1; } cout<<ans; return 0; }
我身后空无一人,我怎敢倒下