zoukankan      html  css  js  c++  java
  • 【习题 5-9 UVA

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    map模拟 map记录每个数组的大小 map ,int>记录数组的某个下标的值。 递归处理嵌套的情况就好

    【代码】

    #include <bits/stdc++.h>
    using namespace std;
    
    string s;
    map <string, int> mmap;
    map <pair<string, int> ,int> mmap2;
    int cur = 0;
    bool ok;
    int gl(int l, int r)
    {
    	return r - l + 1;
    }
    
    pair <string, string> cl(string s)
    {
    	string val = "", temp = "";
    	int now = 0;
    	while (s[now] != '[') val += s[now++];
    	int len = s.size();
    	temp = s.substr(now + 1, gl(now + 1, len - 2));
    	return make_pair(val, temp);
    }
    
    int get_num(string s)
    {
    	if (isdigit(s[0]))
    	{
    		int x = 0, lens = s.size();
    		for (int i = 0; i < lens; i++)
    			x = x * 10 + s[i] - '0';
    		return x;
    	}
    	string val = "";
    	int len = s.size(),l;
    	for (int i = 0; i < len; i++)
    	{
    		if (s[i] == '[')
    		{
    			l = i;
    			break;
    		}
    		val += s[i];
    	}
    	int nex = get_num(s.substr(l + 1, gl(l + 1, len - 2)) );
    	if (nex >= mmap[val] || nex < 0)
    	{
    		ok = false;
    		return -1;
    	}
    	else
    	{
    		if (mmap2.find(make_pair(val, nex)) != mmap2.end())
    		{
    			return mmap2[make_pair(val, nex)];
    		}
    		else
    		{
    			ok = false;
    			return -1;
    		}
    	}
    }
    
    int main()
    {
    	//freopen("F:\rush.txt", "r", stdin);
    	while (cin >> s && s[0] != '.')
    	{
    		mmap.clear();
    		mmap2.clear();
    		ok = true;
    		cur = 1;
    		while (s[0] != '.')
    		{
    			if (!ok)
    			{
    				cin >> s;
    				continue;
    			}
    			int fi = s.find('=', 0);
    			if (fi != -1)// a=b
    			{
    				string s1 = s.substr(0, fi);
    				string s2 = s.substr(fi + 1);
    				
    				pair <string, string> temp = cl(s1);
    				s1 = temp.second;
    				string a = temp.first;
    
    				int x = get_num(s1), y = get_num(s2);
    				if (!ok || x < 0 || x >= mmap[a])
    				{
    					ok = 0;
    					cout << cur << endl;
    					continue;
    				}
    				mmap2[make_pair(a, x)] = y;
    			}
    			else 
    			{
    				pair <string, string> temp = cl(s);
    				mmap[temp.first] = get_num(temp.second);
    			}
    
    			cur++;
    			cin >> s; 
    		}
    		if (ok) cout << 0 << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    第六周测验
    动态获取屏幕尺寸
    推荐免费的svn空间(SVN代码托管)
    Thinking in Java 4th Edition Source Code
    visio 2010 kit tools
    android studio error
    android studio
    Appendix D. Gradle Command Line
    S2S4H整合注意问题
    javascript原型
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7677306.html
Copyright © 2011-2022 走看看