Https://usaco.org/index.php?page=viewproblem2&cpid=712

https://usaco.org/index.php?page=viewproblem2&cpid=712
I had a doubt about this question one implementation that can this be solved using stacks. I am providing the code below. Please help me in this!

int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);

#ifndef ONLINE_JUDGE
freopen(“input.txt”,“r”,stdin);
freopen(“output.txt”,“w”,stdout);
#endif

string s;
cin >> s;

vector<bool>vis(26,false);
stack<char>sa;

int ans = 0;
for (int i = 0 ;i < 52 ; i++){
    int val = s[i]-'A';
    if(!vis[val]){
        sa.push(s[i]);
        vis[val] = true;
    }
    else{
        stack<char>temp;
        while(sa.top()!=s[i] && !sa.empty()){
            temp.push(sa.top());
            sa.pop();
        }
        sa.pop();
        ans += temp.size();
        while(!temp.empty()){
            sa.push(temp.top());
            temp.pop();
        }
    }
}


cout << ans << endl;



return 0;

}

What exactly do you need help with?