话不多说,上代码
例如:输入 12345 32145 输出yes
#include<iostream> #include<stack> using namespace std; bool islegal(char a[],char b[]) { stack<char> s; int sa,sb; sa=strlen(a); sb=strlen(b); if(sa!=sb) return false; for(int i=0,j=0;i<sa;i++) { if(a[i]!=b[j]) { s.push(a[i]); } else { j++; while(!s.empty()&&s.top()==b[j]) { s.pop(); j++; } } } if(s.empty()&&j==sa) return true; else return false; } int main() { int sign=0; char a[100];//优先级 char b[100];//需判断的序列 cin>>a; cin>>b; if(islegal(a,b)) cout<<"yes"<<endl; else cout<<"no"<<endl; return 0; }