int main(void) { int n, i; int * a; int catalan[20] = { 0 };//第20个超出int范围,就不用long long了,反正很快又会超范围的O(∩_∩)O! Catalan(catalan); SeqStack * S; S = (SeqStack *)malloc(sizeof(SeqStack)); printf("请输入堆栈个数(0结束)"); while (scanf("%d", &n) && n > 0) { memset(S->flag, 0, sizeof(S->flag)); S->num = n; S->stack = (int **)calloc(catalan[n], sizeof(int *));//分配二维数组 for (i = 0; i < catalan[n]; i++) { S->stack[i] = (int *)calloc(n, sizeof(int)); } memset(S->flag, 0, sizeof(S->flag)); //S->top = 0;//初始化(默认第一个入栈) for (i = 0; i < n; i++) { scanf("%d", &S->input[i]); } a = Print(S, n - 1, 0, 0);//默认进第一个,则少一个入栈数。 Delete(S, catalan); printf("请输入堆栈个数(0结束)"); *a = -1;//静态变量a归初始化,重复使用^_^ } return 0; }
int main() { int i; //for 'for' loops //level to stop splitting int L, L1; //this means each state has L leaves resulting from k splits cout << "Number of splits? "; cin >> L1; L = L1 + 1; //number of splits int k; //number of distinct states or Catalan number long double Csplit[L]; //total number of possible paths to reach any state at level L unsigned long int totalPaths[L]; //container for states States currStates, nextStates; States::iterator iterCurrStates; State currState, nextState; State::iterator iterCurrState; // a map for the Catalan Coefficients typedef map<State, int, LexicoSorting<State> > StatesMap; StatesMap StateCount; StatesMap::iterator iterStateCount; std::pair<StatesMap::iterator, bool> boolStateCount; //state space at 0 split State state0; state0.push_back(0); StateCount.insert(make_pair(state0,1)); Csplit[0]=1; totalPaths[0]=1; //ofstream to output CatalanCoefficient and CatalanFrequencies to .txt. ofstream oss; clock_t start, end; double timeTaken; //Loop to get Catalan coefficients and frequencies using L-R leaf-depth encoding for (k=1; k < L; k++) //for a given number of splits { //Set up string for filename to output the Catalan coefficients and frequencies string fileNameCC, fileNameCF; fileNameCC = "CatalanCoefficientSplit"; fileNameCF = "CatalanFrequencySplit"; std::ostringstream stm1; //convert k to std::ostringstream start=clock(); //record the time taken to get the Catalan coefficient and frequencies //Get number of nodes, Catalan number and total possible paths for each split Csplit[k] = Catalan(k); totalPaths[k] = factorial(k); cout << "========================================" << endl; cout << "At " << k << " splits:" << endl; cout << Csplit[k] << " distinct states " << "\t" << totalPaths[k] << " possible paths " << endl; //Set up an empty map to store new states StatesMap newStateCount; StatesMap::iterator iterNewStateCount; std::pair<StatesMap::iterator, bool> boolNewStateCount; //for each state in current states map, StateCount for (iterStateCount=StateCount.begin(); iterStateCount != StateCount.end(); iterStateCount++) { currState = iterStateCount->first; //current state in current states map //cout << "Current state:" << getStringState(currState) << endl; //for each node in current state for(int node = 0; node < currState.size(); node++) { //get state for each node in current state nextState = getNextState(currState, node); //cout << "Next state: " << getStringState(nextState) << endl; //insert the new state into newStateMap boolNewStateCount = newStateCount.insert(make_pair(nextState, 1)); //Check if insertion is successful - a unique set will render a successful insertion. if(!(boolNewStateCount.second)) //if state is not unique { //increment by the Catalan coefficient of the current state producing this next state newStateCount[nextState] += StateCount[currState]; //cout << "this state already exist" << endl; } //end of "non-unique state" else { //set the count of this next state as the Catalan coefficient of current state newStateCount[nextState] = StateCount[currState]; //cout << "this is a unique state" << endl; } //end of "unique state" } //end of looping over node in current state } //end of looping through all Ck states in current states map cout << "There are " << newStateCount.size() << endl; //cout << " distinct states with the following breakdown:" << endl; //Catalan Coefficients //Output states with corresponding counts in .txt. stm1 << k; //name file according to the number of splits k fileNameCC += stm1.str(); fileNameCC += ".txt"; oss.open(fileNameCC.c_str()); //set up a vector for the number of paths leading to each state in newStateCount vector<int> StatePathVec; vector<int>::iterator iterStatePathVec; //set up a map for the frequencies corresponding to state paths map<int, int, less<int> > StateFreqMap; map<int, int, less<int> >::iterator iterStateFreqMap; std::pair<map<int, int, less<int> >::iterator, bool> boolStateFreqMap; int Path; for(iterNewStateCount=newStateCount.begin(); iterNewStateCount != newStateCount.end(); iterNewStateCount++) { State thisState = iterNewStateCount->first; //cout << getStringState(thisState) << "\t\t" << iterNewStateCount->second << endl; oss << getStringState(thisState) << "\t" << (iterNewStateCount->second)<< "\n"; //Find frequencies for state paths Path = iterNewStateCount->second; boolStateFreqMap = StateFreqMap.insert(make_pair(Path, 1)); if (!(boolStateFreqMap.second)) //if there is more than one state with Path { StateFreqMap[Path] += 1; } } //close ostream for CatalanCoefficientSplitk.txt oss << flush; oss.close(); cout << "State counts output to " << fileNameCC << endl; //output Catalan Frequencies to.txt fileNameCF += stm1.str(); fileNameCF += ".txt"; oss.open(fileNameCF.c_str()); oss << "Paths" << "\t\t" << "States" << "\n"; //Output to .txt for (iterStateFreqMap = StateFreqMap.begin(); iterStateFreqMap != StateFreqMap.end(); iterStateFreqMap++) { //cout << "There are " << iterStateFreqMap->second << " states with " // << iterStateFreqMap->first << " paths." << endl; oss << iterStateFreqMap->first << "\t\t" << iterStateFreqMap->second << "\n"; } oss << flush; oss.close(); cout << "State frequencies output to " << fileNameCF << endl; //Empty current states map StateCount.clear(); if ( StateCount.empty()) { //set nextStateCount map to be StateCount to continue loop //cout << "space emptied" << endl; StateCount = newStateCount; } else { cout << "space not emptied. Should not proceed." << endl; break; } end=clock(); timeTaken = static_cast<double>(end-start)/CLOCKS_PER_SEC; cout << "Computing time : " <<timeTaken<< " s." << endl; } return 0; }