/////////////////////////////////////////////////////////////// <算法 i~iv> // // Exercise : 4.11 , Page : 103//// exercises description:// 给定两个序列,给出算法来判断是否可以在序列中插入星号,// 使得第一个序列可以生成第二个序列.生成规则由练习4.10来解释.//// zhaoyg 2008.3.5///////////////////////////////////////////////////////////// #include <stdio.h>#include <stdlib.h>#include <string.h> #define MAX 20 int main(){ int source[2][MAX]; int (*p_source)[MAX]=source; int target[MAX]; int *p_target=target; int lenth_between_sur_tar=0 , count_lenth_source=0 ,temp; int i , j , end ,count=0; //printf("enter source string\n"); while (1) { lenth_between_sur_tar=0 , count_lenth_source=0 ; printf("enter source string\n"); while((temp=getchar())!='\n') { p_source[0][count_lenth_source]=temp; p_source[1][count_lenth_source]=0; count_lenth_source++; } count_lenth_source--; printf("enter target string\n"); while ((temp=getchar())!='\n') { *p_target=temp; p_target++; } p_target=target;//////////////////////////////////////////////////////////////////// for (i = 0 ; i<=count_lenth_source ; i++) { for (j=0;j<=count_lenth_source ; j++) { if (p_source[0][j]==p_target[i]) { count=0; for (end = count_lenth_source; end>j && (p_source[1][end] == 0 ); end--) ; lenth_between_sur_tar = end - j; while (j != end) { count +=p_source[1][end]; end-- ; } if (lenth_between_sur_tar != count) { printf("can not!\n"); system("pause"); return 0; } else { p_source[1][end+lenth_between_sur_tar]++; break; } } } } ///////////////////////////////////////////////////////////////// for (int x=0 ; x <=count_lenth_source ; x++) //indicate { printf("%c",p_source[0][x]); for (int j = 0 ; j<p_source[1][x] ; j++) printf("*"); } printf("\n");///////////////////////////////////////////////////////////// } system("pause"); return 0;}

评论