Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

括号匹配的时候注意下,贴下我的AC代码吧

Posted by yzhw at 2009-09-25 00:55:14 on Problem 1114
Source Code

Problem: 1114  User: yzhw 
Memory: 732K  Time: 32MS 
Language: G++  Result: Accepted 

Source Code 
# include <iostream>
# include <cstring>
# include <string>
# include <map>
using namespace std;
map<string,int> refer;
int c=0;
int toknum(char *p,int &mul)
{
	mul=0;
	int i=0;
	for(i=0;i<strlen(p);i++)
		if(p[i]>='0'&&p[i]<='9')
			mul=mul*10+p[i]-48;
		else break;
	if(mul==0)
	{
		mul=1;
	}
	return i;
}
void add(char *str,int data[],int start,int end,int mul)
{
	for(int i=start;i<=end;i++)
	{
		if(str[i]=='(')
		{
			int j;
			int tcount=1;
			for(j=i+1;j<=end;j++)
			{
				if(str[j]=='(') tcount++;
				else if(str[j]==')') tcount--;
				if(!tcount) break;
			}
			int nmul;
			int tempi=j+toknum(&str[j+1],nmul);
			add(str,data,i+1,j-1,mul*nmul);
			i=tempi;
		}
		else if(str[i]>='A'&&str[i]<='Z')
		{
			char tstr[100];
			int tcount=0;
			tstr[tcount++]=str[i];
			int j;
			for(j=i+1;j<=end&&str[j]>='a'&&str[j]<='z';j++)
				tstr[tcount++]=str[j];
			tstr[tcount]='\0';
			int tmul;
			i=j+toknum(&str[j],tmul)-1;
			string ts=string(tstr);
			if(refer.find(ts)==refer.end())
			{
				refer[ts]=++c;
			}
			data[refer[ts]]+=tmul*mul;
		}
		
	}
	
}
void deal(string line,int data[])
{
	char temp[101];
	int num=1;
	strcpy(temp,line.c_str());
	char *p=strtok(temp,"+");
	int multiply=1;
	int start=toknum(p,multiply);
	add(p,data,start,strlen(p)-1,multiply);
	while(p=strtok(NULL,"+"))
	{
		start=toknum(p,multiply);
		add(p,data,start,strlen(p)-1,multiply);
	}
}
int main()
{
	string ori,line;
	int std[1000],now[1000];
	memset(std,0,sizeof(std));
	cin>>ori;
	int num;
	cin>>num;
	deal(ori,std);
	for(int i=1;i<=num;i++)
	{
		cin>>line;
		memset(now,0,sizeof(now));
		bool flag=1;
		deal(line,now);
		for(int i=1;i<=c;i++)
			if(std[i]!=now[i])
			{
				flag=0;
				break;
			}
		if(flag)
		   cout<<ori<<"=="<<line<<endl;
		else
		   cout<<ori<<"!="<<line<<endl;
	}
	return 0;
}
			
	

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator