Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312820
  • 博文数量: 172
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 895
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-09 16:57
文章分类

全部博文(172)

文章存档

2012年(86)

2011年(86)

分类:

2011-12-03 00:01:26

  1. #include <stdio.h>
  2. #include <string.h>

  3. int findSubString(const char *s, int slen, const char *d);
  4. int parseString(char *s, int slen, const char *d);

  5. int
  6. main(void)
  7. {
  8.         char s[] = "ABBCD";
  9.         char d[] = "DA";
  10.         int len = strlen(s);

  11.         printf("s = %s, d = %s\n", s, d);
  12.         printf("Res = %d\n", findSubString(s, len, d));
  13.         printf("s = %s, d = %s\n", s, d);
  14.         printf("Res = %d\n", parseString(s, len, d));
  15.         printf("s = %s, d = %s\n", s, d);

  16.         return (0);
  17. }

  18. //双循环,费时间
  19. int
  20. parseString(char *s, int len, const char *d)
  21. {
  22.         int i = 0;
  23.         int j = 0;

  24.         if (strstr(s, d)) {
  25.                 return (0);
  26.         }

  27.         for (i = 0; i < len; i++) {
  28.                 char ctmp = *s;
  29.                 for (j = 1; j < len; j++) {
  30.                         *(s+j-1) = *(s+j);
  31.                 }
  32.                 *(s+j-1) = ctmp;

  33.                 if (strstr(s, d)) {
  34.                         return (0);
  35.                 } else {
  36.                         return (-1);
  37.                 }
  38.         }

  39.         return (-1);
  40. }

  41. //先分析后处理,空间换时间
  42. int
  43. findSubString(const char *s, int slen, const char *d)
  44. {
  45.         char ss[slen*2];

  46.         strcpy(ss,s);
  47.         strcpy(ss+slen, s);

  48.         if (strstr(ss,d)) {
  49.                 return (0);
  50.         } else {
  51.                 return (-1);
  52.         }
  53. }

阅读(580) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~