码迷,mamicode.com
首页 > 其他好文 > 详细

[005] unique_sub_string

时间:2014-11-09 11:13:04      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   sp   for   strong   

[Description] Given a string, find the largest unique substring.

e.g.  str[] = "asdfghjkkjhgf";

     sub[] = "asd";

[Thought] create an auxiliary space ‘record[256]‘ to record the appearence of characters. O(n)

[Implementation] C code:

 1 #include<stdio.h>
 2 
 3 int uniqueSubStr(const char str[], char unique[])
 4 {
 5         int record[256]={0};
 6         int i,len=0;
 7         for(i=0;str[i]!=\0;i++)
 8         {
 9                 if(0==record[str[i]])
10                 {
11                         record[str[i]]=1;
12                         len++;
13                         unique[len-1]=str[i];
14                 }
15                 else
16                 {
17                         continue;
18                 }
19         }
20         unique[len]=\0;
21         return len;
22 }
23 
24 int main()
25 {
26         char str[]="asdfjlkdfghjc";
27         int size=sizeof(str);
28         char unique[size];
29         int i;
30 
31         printf("Initial string:\n");
32         for(i=0;str[i]!=\0;i++)
33         {
34                 printf("%c",str[i]);
35         }
36         printf("\nThe lagest length of unique substring is: %d\n", uniqueSubStr(str,unique));
37         printf("The unique substring is:\n");
38         for(i=0;unique[i]!=\0;i++)
39         {
40                 printf("%c",unique[i]);
41         }
42         printf("\n");
43         return 0;
44 }

 

[005] unique_sub_string

标签:des   style   blog   io   color   ar   sp   for   strong   

原文地址:http://www.cnblogs.com/lyutian/p/4084685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!