This blog uses some questions on OJ as a carrier to sort out the data structure. Will be updated one after another...
We all know that flexible application of data structures can sometimes simplify answers to some questions.
I. Stack applications
1. njupt oj 1054 (retrieval string judgment)
Determination of the return string: stores half of a string into a stack, and then checks whether the string is a return string from the top of the stack.
/** NJUPTOJ_1054.cpp ** Created on: July 15, May 22, 2014 * Author: pc */# include
# Include
Using namespace std; const int maxn = 300; void toLower (char arr [], int len) {int I; for (I = 0; I <len; ++ I) {if (! Islower (arr [I]) {arr [I] + = 32 ;}} int main () {char a [maxn]; char s [maxn]; while (gets ()! = NULL) {int len = strlen (a); toLower (a, len); int mid = len/2-1; int top = 0; int I; for (I = 0; I <= mid; ++ I) {s [++ top] = a [I];} int next; if (len % 2 = 0) {next = mid + 1;} else {next = mid + 2;} for (I = next; I <= len-1; ++ I) {if (s [top]! = A [I]) {break;} -- top;} if (top! = 0) {printf ("Not Palindrome. \ n");} else {printf ("Bingle! Palindrome. \ n ") ;}} return 0 ;}