您的位置:首页 > 其它

UVA 11988 Broken Keyboard (a.k.a. Beiju Text)

2013-08-17 08:15 337 查看
双端队列。输入一个字符串,包含‘[’和‘]’,表示home和end,也就是说打字的时候光标会跑到最前边或最后边。直接用string模拟的话应该会超时,每次都要改很多东西。

于是乎搞个deque吧。双端队列,两端都可以入队出队的好东东。记录一段字符串的开始和结束的下标,使得他们是连在一起的,不含[ ],注意可能是直接到字符串的最后哦。然后如果遇到home,就push_front,遇到end就push_back,然后一边遍历输出就可以啦。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<deque>
#include<queue>
#include<cmath>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
///OTHER
#define pb(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 100000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 111111;
const int M = 55;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

struct node
{
int a, b;
node(){};
node(int x, int y){a = x, b = y;};
}p;
deque<node> q;
char s
;

int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

int l, x, f;
while(gets(s))
{
l = strlen(s);
x = f = 0;
REP(i, l)
{
if(s[i] == '[' || s[i] == ']' || i == l - 1)
{
if(i == l - 1 && s[i] != '[' && s[i] != ']')i++;
if(f)q.push_front(node(x, i));
else q.push_back(node(x, i));
f = s[i] == '[';
x = i + 1;
}
}
while(!q.empty())
{
p = q.front();
q.pop_front();
FF(i, p.a, p.b)cout << s[i];
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva