Easy List Operations
时间: 1ms 内存:32M
描述:
A list is a sequence of or more elements, expressed in this form: [a 1 , a 2 , a 3 , ... , a n ], where each a iis or more consecutive digits or lowercase letters. i.e. each list begins with a left square bracket,then zero or more elements separated by a single comma, followed by a right square bracket. There willbe no whitespace characters (spaces, TABs etc) within a list.In this problem, we use two list operations: append (++) and remove (--).1. A ++ B: append elements in B to the end of A.2. A -- B: remove all the elements in B, from A. If something appears more than once in B, remove it thatmany times in A. If there are many equal elements in A to choose from, remove them from left to right(until all occurrences are removed, or there is no need to remove more elements).Your task is to write a calculator, evaluating simple expressions or the form "list1 ++ list2" or "list1 --list2".
输入:
There will be at most 10 expressions, one in each line, each having the form "list1 ++ list2" or "list1 --list2", with no more than 80 characters in total (not counting the newline character). There will be exactlytwo spaces in each line: one before and one after the operator. Input ends with a single dot. The input isguaranteed to satisfy the restrictions stated above.
输出:
For each expression, print its result on a single line.
示例输入:
[1,2,3] ++ [1,2,3]
[a,b,c,t,d,e,t,x,y,t] -- [t]
[a,b,c,t,d,e,t,x,y,t] -- [t,t,t,t]
[123] ++ [456]
.
示例输出:
[1,2,3,1,2,3]
[a,b,c,d,e,t,x,y,t]
[a,b,c,d,e,x,y]
[123,456]
提示:
参考答案(内存最优[752]):
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int mod10(int x)
{
int a=1,i;
for(i=0;i<x;i++)
a=(a*10)%101;
return a;
}
int bit(char c)
{
if(c>'C')
{
if(c>'I')
return 3;
else
return 2;
}
else
return 1;
}
int main()
{
int i,j,k,l,len,n,t,sum;
int shu[1000];
char s[100];
scanf("%d",&t);
while(t--)
{
n=0;sum=0;
scanf("%s",s);
len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]>='a')
s[i]-=32;
}
for(i=0,j=0;i<len;i++)
{
n=(s[i]-'A'+1)*(s[i]-'A'+1);
k=bit(s[i]);
j+=k;
for(l=1;l<=k;l++)
{
shu[j-l]=n%10;
n/=10;
}
}
for(i=j-1,k=0;i>=0;i--,k++)
sum+=(shu[i]*mod10(k))%101;
printf("%d\n",sum%101);
}
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char list1[100];
char list2[100];
char sym[2];
while(cin>>list1)
{
if(list1[0]=='.')break;
int i,j,k1,k2;
cin>>sym;
cin>>list2;
if(sym[0]=='+')
{
i=0;
while(list1[i]!=']')
{
cout<<list1[i];
i++;
}
if(strlen(list1)!=2&&strlen(list2)!=2)
cout<<",";
cout<<&list2[1]<<endl;
}
else
{
for(i=1;i<strlen(list2);i++)
{
if(list2[i]!='['&&list2[i]!=']'&&list2[i]!=',')
{
for(j=1;j<strlen(list1);j++)
{
if(list1[j]==list2[i]&&(list1[j-1]=='['||list1[j-1]==',')&&(list2[i-1]=='['||list2[i-1]==','))
{
k1=j;
k2=i;
while(list2[k2]!=','&&list1[k1]!=','&&list2[k2]!=']'&&list1[k1]!=']')
{
k2++;
k1++;
}
if((list2[k2]==','&&list1[k1]==',')||(list2[k2]==']'&&list1[k1]==']'))
{
for(;i<k2;i++,j++)
{
list1[j]=' ';
}
break;
}
else
{
if((list2[k2]==','&&list1[k1]==']')||(list2[k2]==']'&&list1[k1]==','))
{
for(;i<k2;i++,j++)
{
list1[j]=' ';
}
break;
}
}
}
}
}
}
k1=0;
for(i=0;i<strlen(list1);i++)
{
if(list1[i]!=' ') list2[k1++]=list1[i];
}
list2[k1]='\0';
for(i=0;i<=strlen(list2);i++)
{
list1[i]=list2[i];
}
for(i=0;i<strlen(list1);i++)
{
if(list1[i]!=',')
{
cout<<list1[i];
}
else
{
if(i!=0)
{
if((list1[i-1]!=','&&list1[i-1]!='['&&list1[i+1]!=']'))
cout<<list1[i];
}
}
}
cout<<endl;
}
}
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。