[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 44 kinds of operations you can do on it:

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

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

The first and only line contains the string s(1n2×105)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(1m3n)m(1≤m≤3n), the number of operations you will make.

In each of the next mm lines print an operation of the form typei,indexi(1typei4,1indexis)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 44, then indexi should be the index of the first character of the substring “abc” that you want to remove. Indexi is 11−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: acababcabababcϕ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
輸出如何操作才能將給定的字符串變爲空串
格式爲typei  indexitype_i \ \ index_i分別表示第i次操作的操作類型和操作位置。
如果不能則輸出-1

題解:
考慮用棧來維護當前字符串。

首先我們考慮刪去cc
考慮結尾的情況
對於ac來說,我們可以acabaabcaaac→aba→abca→a
讀於bbc來說,我們可以bbcbcbcbbabcbbbbc→bcbc→bbabc→bb
對於abc來說,我們直接刪掉abc
對於bc來說,我們無法刪去c,直接-1

然後處理完所有的c
我們得到了一個ab串
然後對於每個b來說只要找一個a對應即可。

最後對於只剩a的情況,直接刪掉即可, aababcϕ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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章