Exercise 5_9 uva1596 find bug

Really, do not see a topic to write, because Mo was thinking, then still wrote ...
This is sentenced to simulate Stuttgart title
first order how to enter, it is not a judgment on the output of the first string. "" on the line
second, storage, had wanted to use set of classes to finalize a map, to think about the string of numbers that have the string-to-digital not want to write, so I direct deposit string of ... such as da [a] = 15623, and for a number of arrays to be da [a125] = 123 ok to keep this
third kind of processing is s [a [b [b [ ]]]] or the like, the recursive! If you find "[" after not a number, then keep looking down, until there is a number, and then determine whether cross-border and Mo have value, and if so, directly bug to value, and returns returns spaces on the line, because I've got to keep too da [a] and things like that, it will always bug, back on ok
if successful, the return value on the line, this time to pay attention to the first or expression, so the first expression returns are Biography string hey hey hey
fourth note on the right is normal in terms of numbers, so they need a dedicated numeric code that
is good on the code, a little ugly ...

#include <bits/stdc++.h>
using namespace std;
map<string,string> da;
int bug,fir,n;
int val(string s) //判断是不是初始化语句 
{
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		if(s[i]=='=') return 0;
	}
	return 1;
}
string de(string s, int qi)
{
	int wei = 0;
	string name = "",num = "";
	for (int i = qi; i < n; i++)
	{
		if(s[i]=='[' && !wei){
			wei = i+1; name = s.substr(qi,i-qi); i++;
		}
		if(wei){
			if(s[i] >='0' && s[i]<= '9')
			{
				while(s[i] != ']') i++;
					num = s.substr(wei,i-wei);
				break;
			}
			else{
					num = de(s,i);  break;
			}
		}
		//前面为求左边 那种有表达式 的专用代码 
		if(s[i] >='0' && s[i]<= '9')
		{
			num = s.substr(i,n-i);
			return num;
		} // 求右边一串数字的专用代码 
    } 
    //cout<<name<<"  "<<num<<endl; 
	// 一定要注意先判断长度再判断字符串大小!才可完美实现数字比较! 
    if( da[name] <= num && da[name].size() <= num.size()){
    	bug = fir; return " "; //返回个空格反正肯定找不到 
    } //越界 
    name += num; 
    if(!qi) return name; //最前面刚开始可以直接表达式 
    if(!da.count( name ))   //莫得值 
	{
		bug = fir; return " ";
	} 
	return da[name]; //否则都要传值 
	
}
int main()
{
	string  s;
	while(1)
	{
		da.clear();  bug = 0;   fir = 0;   int ok = 1;
		while(cin>>s)
		{
			n = s.size();
			string  name,num;    int wei = 0;
			if(s=="." && fir){
				 break;
			} 
			else if(s=="." && !fir){
				ok = 0; break;
			}
			else{
				fir++; // 第几行程序 
				
				if(val(s)){
					
					for (int i = 0; i < n; i++)
					{
						if(s[i] == '[') {
							wei = i; break;
						}
					}  //找位置
					name = s.substr(0,wei);
					num  = s.substr(wei+1,n-wei-2);
					da[name] = num;   //存下每个 name 的 内存 
				//	cout<<name << "  "<<num<<endl;
				}
				else{
						if(bug) continue;
						string left = de(s,0),right;
						if(bug) continue;
						else 
						{
							for (int i = 0; i < n; i++)
							  if(s[i]=='='){
							  	wei = i+1; break;
							  }
							right = de(s,wei);
						}
						if(bug) continue;
						da[left] = right;  //左边 = 右边 
				//		cout<<left <<"   "<<right<<endl;;
				}

			}	
		}
		if(!ok) break;
		printf("%d\n",bug);		
	}
	return 0;
} 
Published 55 original articles · won praise 1 · views 2653

Guess you like

Origin blog.csdn.net/qq_37548017/article/details/100141531