寧波理工邀請賽 c zoj3185解題報告

來源:互聯網
上載者:User
C - List Operations

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld
& %llu

Submit Status

Description

A list is a sequence of zero or more elements, expressed in this form: [a1, a2, a3, ... , an], where each ai is one 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 will be 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 that many 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".

Input

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 exactly two spaces in each line: one before and
one after the operator. Input ends with a single dot. The input is guaranteed to satisfy the restrictions stated above.

Output

For each expression, print its result on a single line.

Sample Input

[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].

Sample Output

[1,2,3,1,2,3][a,b,c,d,e,t,x,y,t][a,b,c,d,e,x,y][123,456]
水題,字串的處理,容易出錯!
#include <iostream>#include <stdio.h>#include <string>#include<list>using namespace std;int main(){    list<string > q1,q2;    list<string>::iterator ptr1,ptr2;    char c;    string str;    int flag,temp,j;    while(scanf("%c",&c)!=EOF)    {          if(c=='.')            break;          q1.clear();          q2.clear();          while((c=getchar())!=EOF)          {                if(c==']')                  break;                  str="";                  str+=c;                  while((c=getchar())!=',')                  {                        if(c==']')                              break;                        str+=c;                  }                q1.push_back(str);                if(c==']')                        break;          }          getchar();          if((c=getchar())=='+')            flag=1;          else            flag=0;          getchar();          getchar();          getchar();         while((c=getchar())!=EOF)          {                if(c==']')                  break;                  str="";                  str+=c;                  while((c=getchar())!=',')                  { if(c==']')                              break;                     str+=c;                  }                q2.push_back(str);               if(c==']')                        break;          }          getchar();                   if(flag)          {               printf("[");               temp=0;              for(ptr1=q1.begin(),j=1;ptr1!=q1.end();ptr1++,j++)              {                  if(j!=q1.size())                  {                      cout<<*ptr1<<",";                      temp=1;                  }                  else                  {                        cout<<*ptr1;                        temp=1;                  }              }               for(ptr1=q2.begin(),j=1;ptr1!=q2.end();ptr1++,j++)              {                  cout<<","<<*ptr1;//這裡要特別注意,錯了幾次              }              printf("]\n");          }          else          {                for(ptr1=q2.begin();ptr1!=q2.end();ptr1++)                {                      for(ptr2=q1.begin();ptr2!=q1.end();ptr2++)                      {                            if(*ptr1==*ptr2)                            {                                  *ptr2="A";                                 break;                            }                      }                }                temp=0;                for(ptr2=q1.end(),ptr2--,j=q1.size();j>=0;ptr2--,j--)                {                      if(*ptr2!="A")                         {                            temp=j;                            break;                         }                }                                 cout<<"[";                for(ptr1=q1.begin(),j=1;ptr1!=q1.end();ptr1++,j++)                {                      if((*ptr1!="A")&&(j!=temp))                        cout<<*ptr1<<",";                        else if((*ptr1!="A"))                              cout<<*ptr1;                }                printf("]\n");          }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.