倒序搜索字符串

来源:互联网 发布:淘宝面膜机危害 编辑:程序博客网 时间:2024/04/30 06:59

一个字符串由排好序的数构成,在其中查找是否存在另一个字串(也是一个数)。可以用二分法,但是要把所有字符串都转成数。

int main(){  char *strSrc = "1 234 56789 93785935";  char *strTgt = "5935";  int lenTgt = strlen(strTgt)-1;  int indexSrc = strlen(strSrc)-1;  int indexTgt = strlen(strTgt)-1;  bool foundTgt = false;    while (!foundTgt && indexSrc >=0)  {    while ((indexTgt>=0) && (indexSrc>=0) && *(strTgt+indexTgt)==*(strSrc+indexSrc))    {      indexTgt--;      indexSrc--;    }    if ((indexTgt == -1) && (*(strSrc+indexSrc)==' ' || indexSrc == -1))    {      foundTgt = true; // found strTgt in strSrc    }    else if (indexSrc == -1)    {      break; // finished search strSrc    }    else // find next start point in strSrc and reset indices    {      indexTgt = lenTgt;      while(*(strSrc+indexSrc) != ' ' && (indexSrc >= 0))      {        indexSrc--;      }      indexSrc--;    }  }  cout << (foundTgt?"Found!\n":"Not found!\n");  return 0;}





原创粉丝点击