http://www.bnuoj.com/bnuoj/problem_show.php?pid=29373
【题意】:模拟光标输入
【题解】:用双向列表模拟实现,这里用其他模拟会超时,注意内存的释放
【code】:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <list>
#include <string>
#include <string.h> using namespace std; struct Nod
{
char ch;
Nod * second;
Nod * first;
Nod()
{
second=NULL;
first=NULL;
ch=;
}
};
Nod * head,*tail,*now,*temp; char str[]; int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
int i;
scanf("%s",str);
int len = strlen(str);
head = new Nod;
tail = new Nod;
head->second = tail;
tail->first = head;
now = head;
int pos = ;
for(i=;i<len;i++)
{
char ch = str[i];
if(ch=='<')
{
if(now!=head)
{
now = now->first;
// cout<<" sdfsd"<<endl;
}
}
else if(ch=='>')
{
if(now->second!=tail)
{
now = now->second;
}
}
else if(ch=='-')
{
if(now!=head)
{
now->first->second = now->second;
now->second->first = now->first;
temp = now;
free(temp);
now = now->first;
}
}
else
{
temp = new Nod;
temp->ch = ch;
now->second->first = temp;
temp->second =now->second;
now->second = temp;
temp->first = now;
now = now->second;
}
}
now = head->second;
printf("Case %d: ",cas++);
while(now!=tail)
{
printf("%c",now->ch);
now = now->second;
free(now->first);
}
free(now);
putchar();
}
return ;
}