ZOJ1004 Anagrams by Stack
地址: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004
这题我的想法有点不太一样,如果直接用传说中的回溯,入栈出栈应该代码会很短,但是我用的思路比较不太一样。
我先从源字符串的最后开始 找 目标字符串的 第一位,然后利用找到字符在源字符串的当前位置和之前已有的 "i" 个数来确定 这次i的个数 和 这次结果是否合理。
找到第一个后,递归找第二个。具体的思路可以看代码。
代码比较长,思路也比较烦,之后会去网上看些别人的思路。
zoj1004_m1
1 /************************************************************************ 2 Function: ZOJ1004: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 3 History : Create:2012年11月22日23:03:01 (Actually, it is, strictly, a completion time 4 Owner: Yang HaiTao( Dennis Young) 5 Reflection: There is a much easier way to make it(_2) using 回溯. 6 Maybe I don 7 ************************************************************************/ 8 #include <stdio.h> 9 #include <string.h> 10 #include <math.h> 11 12 /*Bound*/ 13 #define kIOArraySize 51 14 #define kWordsArraySize 50 15 /* state */ 16 #define kStateCheckTrue 1 17 #define kStateCheckFalse 0 18 #define kStateNotFound 0 19 #define kStateHasFound 1 20 21 int searchTarstrFromSrcstr(char *srcStr, char *tarStr, int inOutSum); 22 void outputTLArray(int TLArray[kIOArraySize]); 23 void resetTLArray(int TLArray[kIOArraySize]); 24 void resetTLArrayByStep(int TLArray[kIOArraySize]); 25 26 /************************************************************************ 27 * Global Variable * 28 ************************************************************************/ 29 /* 0: exit 1: in 2: out 30 index0: currentIndex 1-1000->in,out to print. 31 */ 32 int printTempLineArray[kIOArraySize] = {0}; 33 char charTable[3] = {'p', 'i', 'o'}; 34 35 int main() 36 { 37 char sourceWords[kWordsArraySize], targetWords[kWordsArraySize]; 38 int state; 39 40 while (gets(sourceWords) != NULL && gets(targetWords) != NULL) 41 { 42 printf("[\n"); 43 state = searchTarstrFromSrcstr(sourceWords, targetWords, 0); 44 printf("]\n"); 45 } 46 47 return 0; 48 } 49 50 int searchTarstrFromSrcstr(char *srcStr, char *tarStr, int inOutSum) 51 { 52 int srcStrLength = 0; 53 int tarStrLength = 0; 54 int foundFlag = kStateNotFound; 55 int i = 0; 56 int k = 0; 57 int howToNameThisNum = 0; 58 char newSrcStr[kWordsArraySize] = {0}; 59 60 srcStrLength = strlen(srcStr); 61 tarStrLength = strlen(tarStr); 62 63 /* error return and end return*/ 64 /* End of the String*/ 65 if (0 == srcStrLength && 0 == tarStrLength) 66 { 67 outputTLArray(printTempLineArray); 68 return kStateCheckTrue; 69 } 70 else if (srcStrLength != tarStrLength) 71 { 72 /* it can not do this with different string length*/ 73 resetTLArray(printTempLineArray); 74 return kStateCheckFalse; 75 } 76 77 /*search the 1st char of target string from last char of the source string*/ 78 for (i=srcStrLength - 1; i>=0; i--) 79 { 80 if (srcStr[i] == tarStr[0]) 81 { 82 foundFlag = kStateHasFound; 83 84 /* howToNameThisNum is a temp number which is important to the next step*/ 85 howToNameThisNum = i + 1 - inOutSum; 86 if (howToNameThisNum < 0) 87 { 88 /*NOTE: <0 means, you need to out more than one char to get the char that you want, 89 but the unnecessary char will make your target word Wrong.*/ 90 /* !!!!Important one!!!!! you need to reset last step when the function will return false*/ 91 resetTLArrayByStep(printTempLineArray); 92 return kStateCheckFalse; 93 } 94 else 95 { 96 /*insert "i" into the print Array*/ 97 for (k = 1; k<= howToNameThisNum; k++) 98 { 99 printTempLineArray[(printTempLineArray[0]+k)] = 1; 100 } 101 /*inset "o" into the print Array*/ 102 printTempLineArray[0] = 103 printTempLineArray[0] + howToNameThisNum+ 1; 104 /*set the first flag number*/ 105 printTempLineArray[printTempLineArray[0]] = 2; 106 107 /*creat new source string 108 The way like this: source:aabcd target:b 109 1.new="" -> 2.new="aa" -> 3.new="aacd" */ 110 strcpy(newSrcStr,""); 111 strncpy(newSrcStr, srcStr, i+1-1); 112 newSrcStr[i] = '\0'; 113 strcat(newSrcStr, srcStr+i+1); 114 115 /*find the next char of the target string from the new source string*/ 116 if (kStateCheckFalse == searchTarstrFromSrcstr 117 (newSrcStr, tarStr+1, inOutSum + howToNameThisNum - 1)) 118 { 119 /*resetTLArray(printTempLineArray); 120 //这里不用resetTLArray只需设置notfound*/ 121 /* you need set the foundFlag back to NotFound, begin the child function return false.*/ 122 foundFlag = kStateNotFound; 123 } 124 else 125 { 126 /* This is IMPORTENT!!!!!!!*/ 127 resetTLArrayByStep(printTempLineArray); 128 } 129 } 130 } 131 } 132 133 if (kStateNotFound == foundFlag) 134 { 135 resetTLArray(printTempLineArray); 136 return kStateCheckFalse; 137 } 138 else 139 return kStateCheckTrue; 140 } 141 142 void outputTLArray(int TLArray[kIOArraySize]) 143 { 144 int i = 0; 145 146 for (i = 1; i <= TLArray[0]; i++) 147 { 148 printf("%c ",charTable[TLArray[i]]); 149 } 150 printf("\n"); 151 } 152 153 void resetTLArray(int TLArray[kIOArraySize]) 154 { 155 int i = 0; 156 for (i = 1; i <= TLArray[0]; i++) 157 { 158 TLArray[i] = 0; 159 } 160 TLArray[0] = 0; 161 } 162 163 void resetTLArrayByStep(int TLArray[kIOArraySize]) 164 { 165 int i = 0; 166 i = TLArray[0]; 167 do 168 { 169 TLArray[i] = 0; 170 i--; 171 TLArray[0]--; 172 } while(i>0 && TLArray[i] != 2); 173 }