|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #define MAXS 30
- char *search(char *s, char *t);
- void ReadString( char s[] ); /* 裁判提供,细节不表 */
- int main()
- {
- char s[MAXS], t[MAXS], *pos;
- ReadString(s);
- ReadString(t);
- pos = search(s, t);
- if ( pos != NULL )
- printf("%d\n", pos - s);
- else
- printf("-1\n");
- return 0;
- }
- /* 你的代码将被嵌在这里 */
- char *search( char *s, char *t )
- {
- int index;int i = 0;
- char *p;
- char *q;
- while(*s != '\0')
- {
- index = 1;
- if(*s == *t)
- {
- p = s;
- q = t;
- while(*q != '\0')
- {
- if((*p != *q) || *p == '\0')
- {
- index = 0;
- break;
- }
- p++;
- q++;
- }
-
- if(index == 0)
- {
- continue;
- }
- else
- return s;
- }
- s++;
- i++;
- }
- return NULL;
-
-
- }
复制代码
帮忙看个题,寻找字串。
6个测试点有两个超时不知道咋回事.
1.长度超过题面MAXS, t在结尾处
2.只差1个字符找不到
本帖最后由 Given2001 于 2020-3-17 16:03 编辑
虽然不知道题目是啥
但是我觉得可能是你的continue跳过了下面的s++
导致指针s的值一直没被改变
这就导致了除非t字符串在s的头部,就会陷入死循环
- if(index == 0)
- {
- continue;
- }
- else
- return s;
复制代码
这一部分改成
试试?
还有,你定义的这个 i 是干什么的,似乎赋值了没有使用过啊
|
|