一開始覺得這個題要平衡樹來做。。。
畢竟要動態的插入。。
畢竟啊,平衡樹的插入是 logn 的是因爲他一開始不知道把元素插入到哪裏。
通過尋找,然後才插入的,這個我一開始就知道他應該插入哪裏,所以,根本用不着平衡樹,鏈表就OK了。
雖然很簡單。。但是是第一次用 STL 的鏈表
棧
#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>
#include <list>
using namespace std;
#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define PII pair<int,int>
#define PDD pair<double,double>
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define MOD 99991
#define MAX 1111111
鏈表
list<char> l;
list<char>::iterator it;
stack<char> f,b;
string s;
int main()
{
READ;
while(cin>>s)
{
while(!f.empty())
f.pop();
while(!b.empty())
b.pop();
for(int i=0;i<s.size();i++)
{
if(s[i]=='L')
if(!f.empty())
{
b.push(f.top());
f.pop();
}
if(s[i]=='R')
if(!b.empty())
{
f.push(b.top());
b.pop();
}
if(s[i]>='a'&&s[i]<='z')
f.push(s[i]);
}
while(!f.empty())
{
b.push(f.top());
f.pop();
}
while(!b.empty())
{
printf("%c",b.top());
b.pop();
}
cout<<endl;
}
return 0;
}
#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>
#include <list>
using namespace std;
#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define PII pair<int,int>
#define PDD pair<double,double>
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 0x3f3f3f3f
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define MOD 99991
#define MAX 1111111
list<char> l;
list<char>::iterator it;
stack<char> f,b;
string s;
int main()
{
READ;
while(cin>>s)
{
l.clear();
//cout<<s<<endl;
it=l.begin();
for(int i=0;i<s.length();i++)
{
//cout<<i<<" ";
if(s[i]=='L')
if(it!=l.begin())
it--;
if(s[i]=='R')
if(it!=l.end())
it++;
if(s[i]>='a'&&s[i]<='z')
l.insert(it,s[i]);
}
for(it=l.begin();it!=l.end();it++)
cout<<(*it);
cout<<endl;
}
return 0;
}