[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' and c 'c' , and there are 4 4 kinds of operations you can do on it:

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

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

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

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

In each of the next m m lines print an operation of the form t y p e i , i n d e x i ( 1 t y p e i 4 , 1 i n d e x i s ) type_i,index_i(1≤type_i≤4,1≤index_i≤|s|) , 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 4 4 , then indexi should be the index of the first character of the substring “abc” that you want to remove. Indexi is 1 1 −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: a c a b a b c a b a b a b c ϕ acab→abcab→ab→abc→ϕ , where ϕ ϕ is the empty string.

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

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

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

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

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

#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;
}
发布了302 篇原创文章 · 获赞 19 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/101977313
今日推荐