[gym102267L]ABC

本文介绍了一个字符串操作问题,目标是通过特定操作将给定的字符串变为空串。文章详细阐述了解决方案,包括使用栈来维护字符串状态,并给出了具体实现代码。

time limit per test : 1.0 s
memory limit per test : 256 MB

You are given a string consisting of letters ′a′,′b′'a', 'b'a,b and ′c′'c'c, and there are 444 kinds of operations you can do on it:

Replace a character ′a′'a'a in the string with "ab""ab""ab".
Replace a character ′b′'b'b in the string with "bc""bc""bc".
Replace a character ′c′'c'c in the string with "ba""ba""ba".
Remove a substring(consecutive characters) "abc""abc""abc" from the string.

Let nnn be the length of the string, can you remove the whole string using at most 3n3n3n operations or state that it’s impossible to do so?
Input

The first and only line contains the string s(1≤n≤2×105)s(1≤n≤2×10^5)s(1n2×105) consisting of characters ′a′,′b′'a', 'b'a,b and ′c′'c'c.
Output

If it’s impossible to remove the whole string print −1-11, otherwise in the first line print m(1≤m≤3n)m(1≤m≤3n)m(1m3n), the number of operations you will make.

In each of the next mmm lines print an operation of the form typei,indexi(1≤typei≤4,1≤indexi≤∣s∣)type_i,index_i(1≤type_i≤4,1≤index_i≤|s|)typei,indexi(1typei4,1indexis), the type of the ith operation and the index of the character you want to do the ith operation on, if the operation is of type 444, then indexi should be the index of the first character of the substring “abc” that you want to remove. Indexi is 111−based and the string is updated after each operation, see example notes for better understanding.
Examples
Input

acab

Output

4
1 1
4 1
2 2
4 1

Input

bac

Output

-1

Note

This is how the string changes in the first example: acab→abcab→ab→abc→ϕacab→abcab→ab→abc→ϕacababcabababcϕ, where ϕϕϕ is the empty string.

题意:
给定一个字符串,只含′a′,′b′,′c′'a','b','c'a,b,c三种字符
你有四种操作
1.将一个’a’,变成"ab"
2.将一个’b’,变成"bc"
3.将一个’c’,变成"ba"
4.删除一个"abc"子串
设字符串长度为n,则你的操作数不能超过3*n
输出如何操作才能将给定的字符串变为空串
格式为typei  indexitype_i \ \ index_itypei  indexi分别表示第i次操作的操作类型和操作位置。
如果不能则输出-1

题解:
考虑用栈来维护当前字符串。

首先我们考虑删去ccc
考虑结尾的情况
对于ac来说,我们可以ac→aba→abca→aac→aba→abca→aacabaabcaa
读于bbc来说,我们可以bbc→bcbc→bbabc→bbbbc→bcbc→bbabc→bbbbcbcbcbbabcbb
对于abc来说,我们直接删掉abc
对于bc来说,我们无法删去c,直接-1

然后处理完所有的c
我们得到了一个ab串
然后对于每个b来说只要找一个a对应即可。

最后对于只剩a的情况,直接删掉即可, a→ab→abc→ϕa→ab→abc→ϕaababcϕ

#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
using namespace std;
char s[200004];
char st[200004],st2[200004];
vector<pa>ans;
int l,t,t2;
int main(){
    scanf("%s",s+1);
    l=strlen(s+1);
    t=0;
    if(s[1]!='a'){
        return puts("-1"),0;
    }
    for(int i=1;i<=l;i++){
        if(s[i]=='c'){
            if(t>1){
                if(st[t-1]=='a'&&st[t]=='b'){
                    ans.push_back({4,t-1});
                    t-=2;
                    continue;
                }
                if(st[t]=='a'){
                    ans.push_back({3,t+1});
                    ans.push_back({2,t+1});
                    ans.push_back({4,t});
                    continue;
                }
                if(st[t-1]=='b'&&st[t]=='b'){
                    ans.push_back({2,t-1});
                    ans.push_back({3,t});
                    ans.push_back({4,t+1});
                    continue;
                }
                return puts("-1"),0;
            }
            else if(t==0){
                return puts("-1"),0;
            }
            else if(t==1){
                if(st[t]=='b'){
                    return puts("-1"),0;
                }
                else{
                    ans.push_back({3,t+1});
                    ans.push_back({2,t+1});
                    ans.push_back({4,t});
                    continue;
                }
            }
        }
        else{
            st[++t]=s[i];
        }
    }
    //for(int i=1;i<=t;i++)cout<<st[i];cout<<endl;
    t2=0;
    for(int i=1;i<=t;i++){
        if(st[i]=='b'){
            if(t2==0){
                return puts("-1"),0;
            }
            else{
                ans.push_back({2,t2+1});
                ans.push_back({4,t2});
                t2--;
            }
        }
        else{
            st2[++t2]=st[i];
        }
    }
    for(int i=1;i<=t2;i++){
        ans.push_back({1,1});
        ans.push_back({2,2});
        ans.push_back({4,1});
    }
    if(ans.size()>l*3)return puts("-1"),0;
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)printf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}
(issacgym) abc@mypc:~/isaacgym/rsl_rl/legged_gym$ python legged_gym/scripts/train.py --task=anymal_c_flat --num_envs=1024 --headless Importing module 'gym_38' (/home/abc/isaacgym/python/isaacgym/_bindings/linux-x86_64/gym_38.so) Setting GYM_USD_PLUG_INFO_PATH to /home/abc/isaacgym/python/isaacgym/_bindings/linux-x86_64/usd/plugInfo.json PyTorch version 2.2.2 Device count 1 /home/abc/isaacgym/python/isaacgym/_bindings/src/gymtorch Using /home/abc/.cache/torch_extensions/py38_cu121 as PyTorch extensions root... Creating extension directory /home/abc/.cache/torch_extensions/py38_cu121/gymtorch... Emitting ninja build file /home/abc/.cache/torch_extensions/py38_cu121/gymtorch/build.ninja... Building extension module gymtorch... Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N) [1/2] c++ -MMD -MF gymtorch.o.d -DTORCH_EXTENSION_NAME=gymtorch -DTORCH_API_INCLUDE_EXTENSION_H -DPYBIND11_COMPILER_TYPE=\"_gcc\" -DPYBIND11_STDLIB=\"_libstdcpp\" -DPYBIND11_BUILD_ABI=\"_cxxabi1011\" -isystem /home/abc/miniconda3/envs/issacgym/lib/python3.8/site-packages/torch/include -isystem /home/abc/miniconda3/envs/issacgym/lib/python3.8/site-packages/torch/include/torch/csrc/api/include -isystem /home/abc/miniconda3/envs/issacgym/lib/python3.8/site-packages/torch/include/TH -isystem /home/abc/miniconda3/envs/issacgym/lib/python3.8/site-packages/torch/include/THC -isystem /home/abc/miniconda3/envs/issacgym/include/python3.8 -D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -std=c++17 -DTORCH_MAJOR=2 -DTORCH_MINOR=2 -c /home/abc/isaacgym/python/isaacgym/_bindings/src/gymtorch/gymtorch.cpp -o gymtorch.o [2/2] c++ gymtorch.o -shared -L/home/abc/miniconda3/envs/issacgym/lib/python3.8/site-packages/torch/lib -lc10 -ltorch_cpu -ltorch -ltorch_python -o gymtorch.so Loading extension module gymtorch... Traceback (most recent call last): File "legged_gym/scripts/train.py", line 36, in <module> from legged_gym.envs import * File "/home/abc/isaacgym/rsl_rl/legged_gym/legged_gym/envs/__init__.py", line 33, in <module> from .base.legged_robot import LeggedRobot File "/home/abc/isaacgym/rsl_rl/legged_gym/legged_gym/envs/base/legged_robot.py", line 46, in <module> from legged_gym.utils.terrain import Terrain File "/home/abc/isaacgym/rsl_rl/legged_gym/legged_gym/utils/__init__.py", line 32, in <module> from .task_registry import task_registry File "/home/abc/isaacgym/rsl_rl/legged_gym/legged_gym/utils/task_registry.py", line 38, in <module> from rsl_rl.runners import OnPolicyRunner File "/home/abc/isaacgym/rsl_rl/rsl_rl/runners/__init__.py", line 8, in <module> from .on_policy_runner import OnPolicyRunner File "/home/abc/isaacgym/rsl_rl/rsl_rl/runners/on_policy_runner.py", line 15, in <module> from rsl_rl.algorithms import PPO, Distillation File "/home/abc/isaacgym/rsl_rl/rsl_rl/algorithms/__init__.py", line 8, in <module> from .distillation import Distillation File "/home/abc/isaacgym/rsl_rl/rsl_rl/algorithms/distillation.py", line 16, in <module> class Distillation: File "/home/abc/isaacgym/rsl_rl/rsl_rl/algorithms/distillation.py", line 19, in Distillation policy: StudentTeacher | StudentTeacherRecurrent TypeError: unsupported operand type(s) for |: 'type' and 'type'
05-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值