Meaning of the questions:
To a string, then there operations q
is to flip the string
Ba
Added character front
Ba
It was added to the final surface character
output final string
Ideas:
Direct step by step operation will certainly timeout
then consider how to get a direct result
of the insertion of each character, it's in place, to see him back on the line several times to flip, flip even times lie ahead position (if
), flipping just behind the odd positions
Code:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
typedef long long ll;
using namespace std;
const int MAXN=2e5+50;
const int inf=0x3f3f3f3f;
const int M=5000*4;
string s;
struct sa{
int op;
int pos;
string ch;
int f;//记录到当前翻转了几次,看后面几次用总的减p[i].f即可
}p[MAXN];
int main()
{
cin>>s;
int q;
cin>>q;
int ans=0;
for(int i=1;i<=q;i++){
cin>>p[i].op;
if(p[i].op==2){
cin>>p[i].pos>>p[i].ch;
p[i].f=ans;
}else ans++;
}
if(ans&1)reverse(s.begin(),s.end());//原字符串的翻转
string cnt="";
for(int i=1;i<=q;i++){
if(p[i].op==2){
if(((ans-p[i].f)%2==1&&p[i].pos==1)||((ans-p[i].f)%2==0&&p[i].pos==2))
s+=p[i].ch;
if(((ans-p[i].f)%2==1&&p[i].pos==2)||((ans-p[i].f)%2==0&&p[i].pos==1))
cnt+=p[i].ch;//先把加到前面的放一起
}
}
reverse(cnt.begin(),cnt.end());//翻转一下,因为每次都往第一个放
cout<<cnt+s<<endl;
return 0;
}
/*
*/