网易2016两道题Assuming Digits && Best Compression Algorithms

趁热来一发。下面代码没有经过测试,因为其实我没有参加。。。


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  
#include
using namespace std;

int num[4];
int solve(string a)
{
	memset(num, 0, sizeof(num));
	for (int i = 0; i < a.length(); i++)
	{
		if (a[i] == '9')
			num[0]++;
		else if (a[i] == '7')
		{
			if (num[0] > 0)
			{
				num[0]--;
				num[1]++;
			}
		}
		else if (a[i] == '0')
		{
			if (num[1] > 0)
			{
				num[1]--;
				num[2]++;
			}
		}
		else if (a[i] == '6')
		{
			if (num[2] > 0)
			{
				num[2]--;
				num[3]++;
			}
		}
	}
	return num[3];
}
int main()
{
	int n;
	cin >> n;
	string a;
	getchar();
	while (n--)
	{
		getline(cin, a);
		cout << solve(a) << endl;
	}
	
	return 0;
}





#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  
#include
using namespace std;
stack st;
void clear()
{
	while (!st.empty())
		st.pop();
}
bool isDigit(char a)
{
	if (a >= '0' && a <= '9')
		return true;
	return false;
}

int solve(string a)
{
	int mark = 0;
	int count[100];
	memset(count, 0, sizeof(count));
	clear();
	for (int i = 0; i < a.length(); i++)
	{
		if (a[i] >= 'A' && a[i] <= 'Z')
		{
			st.push(a[i]);
		}
		else if (isDigit(a[i]))
		{
			int start = i + 1;
			while (start < a.length() && isDigit(a[start]))
				start++;
			string temp = a.substr(i, start - i);
			count[mark] += stoi(temp)+ st.size() - 1;
			i = start - 1;
			clear();
		}
		else if (a[i] == '(')
		{
			count[mark] += st.size();
			mark++;
			clear();
		}
		else if (a[i] == ')')
		{
			count[mark] += st.size();
			clear();

			int sum = count[mark];
			count[mark] = 0;

			int start = i + 1;
			while (start < a.length() && a[start] == ')')
			{
				start++;
				mark--;
				sum += count[mark];
				count[mark] = 0;
			}
			int start2 = start + 1;
			while (start2 < a.length() && isDigit(a[start2]))
				start2++;
			string temp = a.substr(start, start2 - start);
			count[mark] += stoi(temp) * sum;
			i = start2 - 1;
			//mark--;
		}
	} 
	int result = 0;
	for (int i = 0; i < 100; i++)
		result += count[i];
	result += st.size();
	return result;
}

int main()
{
	int n;
	cin >> n;
	string a;
	getchar();
	while (n--)
	{
		getline(cin, a);
		cout << solve(a) << endl;
	}
	return 0;
}


你可能感兴趣的:(POJ水水水)