P10058 Reverse and Rotate 双端队列

题目

前言

我发现其实翻转放后面还是前面都其实没关系的,只要操作正常就没事


然后这个代码 cnt有正负意义的 所以不要用


((sum-x)%p+len)%p变成正的


#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
deque<char>q;
int n;
string s;
long long cnt=0;
string c;
int x;
bool rev=0;
int main()
{
	cin>>s;
	cin>>n;
	int len=s.size();
	for(int i=0;i<len;i++)q.push_back(s[i]);
	for(int i=1;i<=n;i++)
	{
		cin>>c;
		if(c==">")
		{
			cin>>x;
			cnt+=(x%len);
		}
		else if(c=="<")
		{
			cin>>x;
		 cnt=cnt-(x%len);
			//不用用那个%+len%len,因为要考虑cnt为负数 使用pushfrot这些操作
   //可能考虑了 也可以做吧 反正我不会 主要是想学习下双端队列用法	
		}
		else 
		{ 
			rev=!rev;
			cnt=-1*cnt;		
		}		
	}
	if(rev)reverse(q.begin(),q.end());
	if(cnt<0)
	{
		for(int i=1;i<=(-1*cnt)%len;i++)
		{
			q.push_back(q.front());
			q.pop_front();			
		}
	}
  	else if(cnt>0) {
		for(int i=1;i<=cnt%len;i++)
		{
			
			q.push_front(q.back());
			q.pop_back();
			
		}
	}
	for(auto i:q)
	{
		cout<<i;
	}

	
	return 0;
}
posted @ 2025-04-16 19:59  LteShuai  阅读(2)  评论(0)    收藏  举报