POJ3302 Subsequence

来源:互联网 发布:利用热力学数据估算溴 编辑:程序博客网 时间:2024/06/16 03:46
      

Description

Given a string s of length n, a subsequence of it, is defined as another string s' = su1su2...sum where 1 ≤ u1 < u2 < ... < umn and si is the ith character of s. Your task is to write a program that, given two strings s1 and s2, checks whether either s2 or its reverse is a subsequence of s1 or not.

Input

The first line of input contains an integer T, which is the number of test cases. Each of the next T lines contains two non-empty strings s1 and s2 (with length at most 100) consisted of only alpha-numeric characters and separated from each other by a single space.

Output

For each test case, your program must output "YES", in a single line, if either s2 or its reverse is a subsequence of s1. Otherwise your program should write "NO".

Sample Input

5arash aaharash hsrkick kkcA aa12340b b31

Sample Output

YESYESNONOYES
#include<stdio.h>#include<string.h>#define MAXC 105int isSub(char* a,char* b){int i,j,alen,blen;alen=strlen(a);blen=strlen(b);if(alen<blen) //前边的竟然小些return 0;//顺着j=0;    for(i=0;i<alen;i++)if(a[i]==b[j])j++; //移到b下一个字符if(b[j]=='\0')return 1;//逆着j=blen-1;for(i=0;i<alen;i++)if(a[i]==b[j])j--;if(j==-1)return 1;return 0;}int main(){int n;int i,j;char a[MAXC],b[MAXC];scanf("%d",&n);if(n>0){for(i=0;i<n;i++){scanf("%s %s",a,b);if(isSub(a,b))printf("YES\n");elseprintf("NO\n");}}return 0;}


	
				
		
原创粉丝点击