题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5071
题意:
模拟QQ窗口聊天序列的若干操作,输出结果。
算法:
模拟。
直接用一个vector或者queue来模拟窗口序列。
另外找一个数组或map来记录说的词的数量。
额外用一个变量来记录当前“always on top”的是谁。
代码:
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
char command[20];
vector<int> a;
map<int, long long> cot;
int top;
inline int get_top()
{
if (top != -1)
{
return top;
}
if (!a.empty())
{
return a[0];
}
return -1;
}
inline int index_of(int x)
{
for (int i = 0; i < a.size(); i ++)
{
if (a[i] == x)
{
return i;
}
}
return -1;
}
inline void move_first(int x)
{
int tmp = a[x];
for (int i = x; i > 0; i --)
{
a[i] = a[i - 1];
}
a[0] = tmp;
}
int main()
{
int cas;
scanf("%d", &cas);
while (cas --)
{
int n;
scanf("%d", &n);
a.clear();
cot.clear();
top = -1;
for (int tot = 1; tot <= n; tot ++)
{
scanf("%s", command);
printf("Operation #%d: ", tot);
if (command[0] == 'A')
{
int x;
scanf("%d", &x);
if (cot.find(x) != cot.end())
{
puts("same priority.");
}
else
{
a.push_back(x);
cot[x] = 0;
puts("success.");
}
}
else if ((command[0] == 'C') && (command[1] == 'l'))
{
int x;
scanf("%d", &x);
if (cot.find(x) != cot.end())
{
if (top == x)
{
top = -1;
if (cot[x] > 0)
{
printf("Bye %d: %I64d\n", x, cot[x]);
}
}
printf("close %d with %I64d.\n", x, cot[x]);
for (int i = index_of(x); i + 1 < a.size(); i ++)
{
a[i] = a[i + 1];
}
a.pop_back();
cot.erase(x);
}
else
{
puts("invalid priority.");
}
}
else if ((command[0] == 'C') && (command[1] == 'h') && (command[2] == 'a'))
{
int c;
scanf("%d", &c);
if (!a.empty())
{
cot[get_top()] += c;
puts("success.");
}
else
{
puts("empty.");
}
}
else if (command[0] == 'R')
{
int x;
scanf("%d", &x);
if (x <= a.size())
{
move_first(x - 1);
puts("success.");
}
else
{
puts("out of range.");
}
}
else if (command[0] == 'P')
{
if (!a.empty())
{
int x = 0;
for (int i = 0; i < a.size(); i ++)
{
if (a[i] > a[x])
{
x = i;
}
}
move_first(x);
puts("success.");
}
else
{
puts("empty.");
}
}
else if ((command[0] == 'C') && (command[1] == 'h') && (command[2] == 'o'))
{
int x;
scanf("%d", &x);
int index = index_of(x);
if (index != -1)
{
move_first(index);
puts("success.");
}
else
{
puts("invalid priority.");
}
}
else if (command[0] == 'T')
{
int x;
scanf("%d", &x);
if (cot.find(x) != cot.end())
{
top = x;
puts("success.");
}
else
{
puts("invalid priority.");
}
}
else if (command[0] == 'U')
{
if (top != -1)
{
top = -1;
puts("success.");
}
else
{
puts("no such person.");
}
}
}
if (top != -1)
{
if (cot[top] > 0)
{
printf("Bye %d: %I64d\n", top, cot[top]);
}
}
for (int i = 0; i < a.size(); i ++)
{
int x = a[i];
if ((x != top) && (cot[x] > 0))
{
printf("Bye %d: %I64d\n", x, cot[x]);
}
}
}
return 0;
}