有沒有人可以告訴我什麼是解決問題的最佳選擇數據結構@http://acm.timus.ru/problem.aspx?space=1&num=1027。解決Timus Nr#1027的數據結構的最佳選擇是什麼?
複製在這裏以供參考。
問題
正確d的文本++程序包含一個符號部分,算術表達式 和評論。評論可能會出現在任何地方,並可能包含任何符號 。評論始終由一對符號 (*和由一對符號*關閉)打開每個評論必須是 已關閉。在d ++的算術表達式總是被打開「(」,是 通過關閉「)」,並且可以僅包含符號「= + - /)(」。和 「行結束」的符號的算術表達式不能從 這一對符號開始(),您可以在 算術表達式中運行嵌入的括號,在這種情況下,這些括號需要平衡 這意味着「((1)))」以及「 (23))((+)」是不正確的 算術表達式。算術表達式是正確的,如果和 僅當支架放置正確。最後, 程序文本的所有其餘部分(拒絕所有意見的結果,算術 表達式來自程序的初始文本)可能包含每個 符號不包括「(」和「)」。我們特別要注意的是, 空格可能在程序文本中的任意位置,除非在算術表達式中出現 。
散列法會是有用的?
我的方法是如下(還存在一些漏洞,集中在DS部分)
#include <stdio.h>
/* enable to get debug print statements */
#define DEBUG_ENABLE 0
#define INSUFFICIENT_BUFFER -1
#define BUFSIZE 20
char buf[BUFSIZE];/*buffer for ungetch*/
int bufp=0;/*next free position in buf*/
/*get a(possibly pushed-back)character*/
int mygetch(FILE *infile)
{
return(bufp>0)? (buf[--bufp]): (getc(infile));
}
/*push a character back on input*/
int ungetch(char c)
{
if(bufp >= BUFSIZE)
{
printf("ungetch:too many characters - increase the stack buffer size\n");
return INSUFFICIENT_BUFFER;
}
else
{
buf[bufp++]=c;
return 0;
}
}
enum CharType
{
isAlphabet=0,
isNumber,
isSpace,
isNewline,
isOperator,
isOpeningBrace,
isClosingBrace,
isStar,
isOther
};
enum
{
False=0,
True
};
/* return different codes for different types of input characters*/
int getCharType(char ch)
{
if((ch>='A'&&ch<='Z') || (ch>='a'&&ch<='z'))
{
return isAlphabet;
}
else if(ch=='+'||ch=='-'||ch=='/'||ch=='=')
{
return isOperator;
}
else if(ch>='0'&& ch<='9')
{
return isNumber;
}
else if(ch=='*')
{
return isStar;
}
else if(ch=='(')
{
return isOpeningBrace;
}
else if(ch==')')
{
return isClosingBrace;
}
else if(ch==' ')
{
return isSpace;
}
else
{
return isOther;
}
}
int parseInputFile(FILE *infile)
{
int ArthExpScanning = 0;
int CmmntScanning = False;
int ch,chtmp;
while((ch=mygetch(infile))!=EOF)
{
#if DEBUG_ENABLE
printf("%c",ch);
#endif /*DEBUG_ENABLE*/
switch(getCharType(ch))
{
/*Arithmetic Expression or possibly comment starts here*/
case isOpeningBrace :
if((chtmp=mygetch(infile))!=EOF)
{
if(getCharType(chtmp)== isStar)
{
CmmntScanning = True;
#if DEBUG_ENABLE
printf("\nCmmnt Scanning = True\n");
#endif /*DEBUG_ENABLE*/
}
else if (CmmntScanning == False)
{
ArthExpScanning += 1;
#if DEBUG_ENABLE
printf("\nArthExpScanning = %d\n",ArthExpScanning);
#endif /*DEBUG_ENABLE*/
if(ungetch(chtmp) == INSUFFICIENT_BUFFER)
{
return (0);
}
}
}
break;
/*Arithmetic Expression possibly closes here */
case isClosingBrace :
if(CmmntScanning == False)
{
ArthExpScanning -= 1;
#if DEBUG_ENABLE
printf("\nArthExpScanning = %d\n",ArthExpScanning);
#endif /*DEBUG_ENABLE*/
}
#if DEBUG_ENABLE
if(ArthExpScanning < 0)
{
printf("\nerror here!!\n");
}
#endif /*DEBUG_ENABLE*/
break;
case isStar :
if((chtmp=mygetch(infile))!=EOF)
{
if((getCharType(chtmp)== isClosingBrace) && (CmmntScanning == True))
{
CmmntScanning = False;
#if DEBUG_ENABLE
printf("\nCmmnt Scanning = False\n");
#endif /*DEBUG_ENABLE*/
}
else
{
if(ungetch(chtmp) == INSUFFICIENT_BUFFER)
{
return (0);
}
}
}
break;
case isSpace :
if((CmmntScanning == False) && (ArthExpScanning != 0))
{
/* Space not allowed while scanning arith exp */
#if DEBUG_ENABLE
printf("NO \n");
#endif /*DEBUG_ENABLE*/
return 0;
}
break;
case isAlphabet :
case isOperator :
case isNumber :
case isNewline :
default:
break;
}
}
if((ArthExpScanning == 0) && (CmmntScanning == False))
{
/* if there are no open braces and comments left after parsing the entire
file return success*/
return 1;
}
else
{
return 0;
}
}
int main(int argc,char *argv[])
{
FILE *infile;
if(argc != 2)
{
printf("Correct usage is : D++ExpParser inputfilename\n");
return (-1);
}
if((infile = fopen(argv[1],"r")) == NULL)
{
printf("Not able to open file : %f\n",argv[1]);
return (-2);
}
if(parseInputFile(infile))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
fclose(infile);
}
不明白你的問題。在什麼情況下進行散列?你是否想保留你將要解析的符號的地圖。請清楚地向社區中的任何人解釋您的問題,以幫助您。否則,你的問題可能會下降。謝謝。 – Gangadhar 2012-02-28 13:43:50
我不知道散列的概念很好,所以只是在這裏吐口水 – Hari 2012-02-28 13:48:48