标签: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 }
标签:des style blog io color ar sp for strong
原文地址:http://www.cnblogs.com/lyutian/p/4084685.html