#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=1000000007;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

string s;
map<char,int> ma;
bool f = true;

P dfs(int l,int r){
    if(!f) return mp(-1,-1);
    if(r - l <= 1) return mp(ma[s[l]],0);
    int cnt = 0;
    int idx = -1;
    for (int i = l; i < r; i++) {
        if(cnt == 1 && s[i] == '-'){
            idx = i;
            break;
        }
        else if(s[i] == '[') cnt++;
        else if(s[i] == ']') cnt--;
    }
    auto ll = dfs(l+1,idx), rr = dfs(idx+1,r-1); 
    if(ll.fs == ll.sc && rr.fs == rr.sc) f = false;
    else if(ll.fs == ll.sc) return mp(rr.fs,rr.sc+1);
    else if(rr.fs == rr.sc) return mp(ll.fs,ll.sc+1);
    else f = false;
    return mp(-1,-1);
}


int main(){
    cin >> s;
    for (int i = 0; i < s.length(); i++) {
        int a = s[i] - 'a';
        if(0<=a&&a<26){
            char ch;cin >> ch;
            int num;cin >> num;
            ma[ch] = num;
        }
    }
    auto r = dfs(0,s.length());
    if(f&&r.fs == r.sc) puts("Yes");
    else puts("No");
    return 0;
}

