1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <stdio.h> char word[101]; char word_line[101]; char result[101]; void set( int n); int count( void ); //문자열의 길이 void search( int n); //주어진 단어 바로 다음에 나타나는 단어 찾기 void line_set( int a, int n); char next_word( int a, int n); int main( void ) { int n, i; scanf ( "%s" , word); n=count(); set(n); search(n); result[n+1]= '\0' ; printf ( "%s" , result); return 0; } int count( void ) { int i, cnt=0; for (i=0; word[i]!= '\0' ; i++) cnt++; return cnt-1; } void set( int n) { int i; for (i=0; i<=(n+1); i++) word_line[i]= '0' ; } void search( int n) { int i, a=-1; for (i=n; i>0; i--) { if (word[i]>word[i-1]) { a=i-1; break ; } } if (a==-1) { for (i=0; i<=n; i++) { result[i]=word[i]; } } else { for (i=0; i<a; i++) { result[i]=word[i]; } line_set(a, n); char next=next_word(a, n); result[a]=next; int b=0; for (i=1; i<=(n-a+1); i++) { if (word_line[i+b]==next) b=1; result[i+a]=word_line[i+b]; } } } void line_set( int a, int n) { int i, j, cnt=0, c=0; for (i=a; i<=n; i++) { cnt=1; c=0; for (j=a; j<=n; j++) { if (word[i]>word[j]) cnt++; } if (word_line[cnt]== '0' ) { word_line[cnt]=word[i]; } else { while (word_line[cnt+c]!= '0' ) { c++; } word_line[cnt+c]=word[i]; } } } char next_word( int a, int n) { int i; char min= 'Z' ; for (i=1; i<=(n-a+1); i++) { if (word_line[i]>word[a] && min>word_line[i]) { min=word_line[i]; } } return min; } |
2018.05.26 12:49
정올 - 실전대비 - 단어맞추기(1034)
조회 수 727 추천 수 0 댓글 0