UVA10340 子序列

mac2022-06-30  110

入门书习题

方法: 遍历s,然后在t里面找,找到了就继续遍历s,如果s遍历完了全找到了就ok,只要有一个字母在t里面找不到,就不ok。

代码:

#include<stdio.h> #include<iostream> #include<string.h> using namespace std; const int maxn=1005; char s[maxn],t[maxn]; int main(){ int T; cin>>T; while(T--){ cin>>s; cin>>t; int c=0; for(int i=0;i<strlen(s);i++){ int k=s[i]; for(int j=c;j<strlen(t);j++){ if(t[j]==k){ if(i==strlen(s)-1)cout<<"Y"; c=j; break; } if(j==strlen(t)-1){ cout<<"N";i=strlen(s)-1;break;} } } } return 0; }
最新回复(0)