#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
struct Tree
{
	char win;
	int winnum;
};
map<int, Tree> tree;
int numchar = 0;
int hash1[2000];
int res = 0;
void judge(int i)
{
	if (tree[i].win==0)
	{
		judge(i * 2);
		judge(i * 2 + 1);
		if (tree[i * 2].winnum&&tree[i * 2 + 1].winnum)
			res = 1;
		if (tree[i * 2].winnum < 0 || tree[i * 2 + 1].winnum < 0)
			res = 1;
		if (tree[i * 2].winnum > tree[i * 2 + 1].winnum)
		{
			tree[i].win = tree[i * 2].win;
			tree[i].winnum = tree[i * 2].winnum - 1;
		}
		else
		{
			tree[i].win = tree[i * 2 + 1].win;
			tree[i].winnum = tree[i * 2 + 1].winnum - 1;
		}
		if (tree[i].winnum < 0)
			res = 1;
		if (i == 1 && tree[i].winnum)
			res = 1;
	}
}
int main()
{
	string str;
	cin >> str;
	int flag = 0;
	int now = 1;
	memset(hash1, 0, sizeof(hash1));
	for (int i = 0; i < str.size(); i++)
	{
		char s = str[i];
		if (s == '[')
		{
			flag++;
			now *= 2;
		}
		else if (s == '-')
		{
			now += 1;
		}
		else if (s == ']')
		{
			now /= 2;
			tree[now].win = 0;
			tree[now].winnum = -1;
		}
		else
		{
			tree[now].win = s;
			tree[now].winnum = -1;
			numchar++;
			hash1[s - 'a'] = now;
		}
		//cout << now << endl;
	}
	//cout << numchar << endl;
	for (int i = 0; i < numchar; i++)
	{
		char c;
		int x;
		cin >> c >> x;
		tree[hash1[c - 'a']].winnum = x;
	}
	judge(1);
	if (res)
		cout << "No" << endl;
	else
		cout << "Yes" << endl;
	return 0;
}
