您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 数据结构(第二版)习题答案第4章
µÚ4ÕÂ×Ö·û´®¡¢Êý×éºÍÌØÊâ¾ØÕó4.1Ï¡Êè¾ØÕó³£ÓõÄѹËõ´æ´¢·½·¨ÓУ¨ÈýÔª×é˳Ðò´æ´¢£©ºÍ£¨Ê®×ÖÁ´±í£©Á½ÖÖ¡£4.2ÉèÓÐÒ»¸ö10¡Á10µÄ¶Ô³Æ¾ØÕóA²ÉÓÃѹËõ·½Ê½½øÐд洢£¬´æ´¢Ê±ÒÔ°´ÐÐÓÅÏȵÄ˳Ðò´æ´¢ÆäÏÂÈý½ÇÕ󣬼ÙÉèÆäÆðʼԪËØa00µÄµØַΪ1£¬Ã¿¸öÊý¾ÝÔªËØÕ¼2¸ö×Ö½Ú£¬Ôòa65µÄµØַΪ£¨53£©¡£4.3Èô´®S=¡°software¡±£¬Æä×Ó´®µÄÊýĿΪ£¨36£©¡£4.4³£¶ÔÊý×é½øÐеÄÁ½ÖÖ»ù±¾²Ù×÷Ϊ£¨·ÃÎÊÊý¾ÝÔªËØ£©ºÍ£¨ÐÞ¸ÄÊý×éÔªËØ£©¡£4.5Òª¼ÆËãÒ»¸öÊý×éËùÕ¼¿Õ¼äµÄ´óС£¬±ØÐëÒÑÖª£¨Êý×é¸÷άÊý£©ºÍ£¨Ã¿¸öÔªËØÕ¼ÓõĿռ䣩¡£4.6¶ÔÓÚ°ë´ø¿íΪbµÄ´ø×´¾ØÕó£¬ËüµÄÌصãÊÇ£º¶ÔÓÚ¾ØÕóÔªËØaij£¬ÈôËüÂú×㣨|i-j|b£©£¬Ôòaij=0¡£4.7×Ö·û´®ÊÇÒ»ÖÖÌØÊâµÄÏßÐÔ±í£¬ÆäÌØÊâÐÔÌåÏÖÔÚ£¨¸ÃÏßÐÔ±íµÄÔªËØÀàÐÍΪ×Ö·û£©¡£4.8ÊÔ±àдһ¸öº¯Êý£¬ÊµÏÖÔÚ˳Ðò´æ´¢·½Ê½ÏÂ×Ö·û´®µÄstrcompare£¨S1£¬S2£©ÔËËã¡£¡¾´ð¡¿£º#includestdio.h#includestring.h#defineMAXSIZE100typedefstruct{charstr[MAXSIZE];intlength;}seqstring;/*º¯Êýstrcompare()µÄ¹¦ÄÜÊÇ£ºµ±s1s2ʱ·µ»Ø1£¬µ±s1==s2ʱ·µ»Ø0£¬µ±s1s2ʱ·µ»Ø-1*/intstrcompare(seqstrings1,seqstrings2){inti,m=0,len;len=s1.lengths2.length?s1.length:s2.length;for(i=0;i=len;i++)if(s1.str[i]s2.str[i]){m=1;break;}elseif(s1.str[i]s2.str[i]){m=-1;break;}returnm;}intmain(){seqstrings1,s2;inti,m;printf(inputchartos1:\n);gets(s1.str);s1.length=strlen(s1.str);printf(inputchartos2:\n);gets(s2.str);s2.length=strlen(s2.str);m=strcompare(s1,s2);if(m==1)printf(s1s2\n);elseif(m==-1)printf(s2s1\n);elseif(m==0)printf(s1=s2\n);}4.9ÊÔ±àдһ¸öº¯Êý£¬ÊµÏÖÔÚ˳Ðò´æ´¢·½Ê½ÏÂ×Ö·û´®µÄreplace£¨S£¬T1£¬T2£©ÔËËã¡£¡¾²Î¿¼³ÌÐò1¡¿£º/*±¾³ÌÐòÓÃÀ´ÔÚ˳Ðò´æ´¢ÏÂÓÿìËÙÆ¥ÅäģʽʵÏÖÔÚ×Ö·û´®SÖÐÓÃT2Ìæ»»ËùÓÐT1³öÏֵĿÉÄÜ*/#includemalloc.h#includestring.h#includestdio.h#definemaxsize100typedefstruct{charstr[maxsize];intlength;}seqstring;//Çónext[]º¯Êývoidgetnext(seqstring*p,intnext[]){inti,j;next[0]=-1;i=0;j=-1;while(ip-length){if(j==-1||p-str[i]==p-str[j]){++i;++j;next[i]=j;}elsej=next[j];}}//¿ìËÙģʽƥÅäËã·¨intkmp(seqstring*t,seqstring*p,intnext[],inttemppos){inti,j;i=temppos,j=0;while(it-length&&jp-length){if(j==-1||t-str[i]==p-str[j]){i++;j++;}elsej=next[j];}if(j==p-length)return(i-p-length);elsereturn(-1);}//Ìæ»»º¯Êývoidtakeplace(seqstring*S,seqstring*T1,seqstring*T2){intnext[maxsize],temppos=0,pos,i;intlent1,lent2;lent1=strlen(T1-str);//ÇóT1´®³¤¶Èlent2=strlen(T2-str);//ÇóT2´®³¤¶Ègetnext(T1,next);//ÇóT1´®µÄnext[]ÏòÁ¿do{pos=kmp(S,T1,next,temppos);//¿ìËÙģʽƥÅätemppos=pos+T1-length;if(pos!=-1)//Æ¥Åä³É¹¦{if(lent1lent2)//T1´®³¤¶È´óÓÚT2´®³¤¶È{for(i=temppos;iS-length;i++)//Ç°ÒÆS-str[i-lent1+lent2]=S-str[i];for(i=0;iT2-length;i++)//Ìæ»»S-str[pos+i]=T2-str[i];S-length-=lent1-lent2;//Ð޸ij¤¶È}elseif(lent2lent1)//T2´®³¤¶È´óÓÚT1´®³¤¶È{for(i=S-length-1;i=temppos;i--)//ºóÒÆÁô¿ÕS-str[i+lent2-lent1]=S-str[i];for(i=0;iT2-length;i++)//Ìæ»»S-str[pos+i]=T2-str[i];S-length+=lent2-lent1;//Ð޸ij¤¶È}else//T1³¤¶ÈÓëT2³¤¶ÈÏàµÈ{for(i=0;iT2-length;i++)S-str[pos+i]=T2-str[i];}temppos=pos+T2-length;//ÐÞ¸ÄÏÂÒ»´ÎģʽƥÅäµÄÆðµãλÖÃ}}while(pos!=-1);S-str[S-length]='\0';}intmain(){seqstring*S,*T1,*T2;printf(\n\n±¾³ÌÐòÓÃÀ´ÊµÏÖ×Ö·û´®Ìæ»»£¬½«SÖÐÓÃT2Ìæ»»T1ËùÓгöÏÖ\nThisprogramisusedforcharactersbatchtakeingplace,TheT1charactersbatchwilltakeplacealltheT2'sappearancesincharactersbatchS:\n\n);printf(ÇëÊäÈëSÖеÄ×Ö·û(pleseinputcharactersbatchS:)\n);S=(seqstring*)malloc(sizeof(seqstring));T1=(seqstring*)malloc(sizeof(seqstring));T2=(seqstring*)malloc(sizeof(seqstring));scanf(%s,S-str);S-length=strlen(S-str);printf(ÊäÈëÒª±»Ìæ»»µÄ´®(inputT1):\n);scanf(%s,T1-str);T1-length=strlen(T1-str);printf(ÊäÈëÌæ»»´®(inputT2):\n);scanf(%s,T2-str);T2-length=strlen(T2-str);takeplace(S,T1,T2);printf(Ìæ»»ºóµÄ×Ö·û´®Îª£º\n);printf(%s\n,S-str);free(S);free(T1);free(T2);}¡¾²Î¿¼³ÌÐò2¡¿£º#includestdio.h#defineMAXSIZE100typedefstruct{charstr[MAXSIZE];intlength;}seqstring;voidgetnext(seqstringp,intnext[])//ÇóģʽµÄnextº¯Êý{inti,j;next[0]=-1;i=0;j=-1;while(ip.length){if(j==-1||p.str[i]==p.str[j]){++i;++j;next[i]=j;}elsej=next[j];}}voidreplace(seqstring*s,seqstringt1,seqstringt2,intnext[]){inti,j,k,c,m;i=0;j=0;k=0;while(is-length){j=0;while(is-length&&jt1.length){if(j==-1||s-str[i]==t1.str[j]){i++;j++;}elsej=next[j];}if(j==t1.length)//Æ¥Åä³É¹¦{c=i-t1.length;if(t1.length==t2.length)//Á½´®³¤¶ÈÏàµÈÖ±½ÓÌæ»»for(k=0;kt2.length;k++)s-str[c+k]=t2.str[k];elseif(t1.lengtht2.length){for(m=s-length-1;mi-1;m--)s-str[t2.length-t1.length+m]=s-str[m];//ºóÒÆÁô¿Õfor(k=0;kt2.length;k++)s-str[c+k]=t2.str[k];s-length=s-length-t1.length+t2.length;s-str[s-length]='\0';}else{for(m=i-1;ms-length;m++)//Ç°ÒÆs-str[m-t1.length+t2.length]=s-str[m];for(k=0;kt2.length;k++)s-str[c+k]=t2.str[k];s-length=s-length-t1.length+t2.length;s-str[s-length]='\0';}i=i+t2.length-t1.length;}i++;}}intmain(){intnext[MAXSIZE];seqstrings,t1,t2;printf(Inputstrings:);//ÊäÈëÖ÷´®gets(s.str);printf(\nInputstringt1:);//ÊäÈëģʽ´®gets(t1.str);printf(\nInputstringt2:);//ÊäÈëÄâÌæ»»µÄ´®gets(t2.str);s.length=strlen(s.str);t1.length=strlen(t1.str);t2.length=strlen(t2.str);getnext(t1,next);replace(&s,t1,t2,next);puts(s.str);}4.10ÊÔ±àдһ¸öº¯Êý£¬ÊµÏÖÔÚÁ´Ê½´æ´¢·½Ê½ÏÂ×Ö·û´®µÄstrcompare£¨S1£¬S2£©ÔËËã¡£
本文标题:数据结构(第二版)习题答案第4章
链接地址:https://www.777doc.com/doc-3171953 .html