Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370609
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-27 09:13:01

  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. }

阅读(1667) | 评论(0) | 转发(1) |
0

上一篇:抽象基类

下一篇:大小端问题

给主人留下些什么吧!~~