why I can't find subarray correctly?

Revision en5, by DNNJFM, 2016-07-31 19:30:36

To clarify, "subarray" is defined like, for example:

if A="abc" B="??a???b????c???" then A is subarray of B.

I have two version to check if A is subarray of B, but versionI get AC, versionII get WA:

version I. iterate the bigger string B.

curA=0;
for(int i=0;i<B.size;i++){
    c=B[i];
    if(curA<A.size && A[curA]==c )curA++;
}
if(curA==A.size)return true;
else return false;

version II. iterate the smaller string A

curB=0;
for(int i=0;i<A.size;i++){
    c=A[i];
    while(curB<B.size && B[curB]!=c ) curB++;
    if(curB==B.size)return false;
}
return true;

**** Why versionI is correct, while versionII is wrong? anyone please explain or give an example to show that versionII is wrong, sincerly thanks in advace!

Tags subarray

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English DNNJFM 2016-07-31 19:30:36 2 Tiny change: '&& B[curB]==c ) curB+' -> '&& B[curB]!=c ) curB+' (published)
en4 English DNNJFM 2016-07-31 19:25:51 14 Tiny change: 'of B, but I get AC, II get WA:' -> 'of B, but versionI get AC, versionII get WA:' (saved to drafts)
en3 English DNNJFM 2016-07-31 19:24:47 537 Tiny change: ' string.\n~~~~~\nc' - (published)
en2 English DNNJFM 2016-07-31 19:15:22 201
en1 English DNNJFM 2016-07-31 19:14:05 149 Initial revision (saved to drafts)