首页 > 代码库 > 【HDOJ】3242 List Operations

【HDOJ】3242 List Operations

注意以下数据:
[] ++ []
[] -- [1,2]
[1,2] ++ []
[] ++ [1]
[2] ++ []
[2,3] -- []
结果是
[]
[]
[1,2]
[1]
[2]
[2,3]

 1 /* 3242 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5  6 #define MAXN 205 7  8 typedef struct { 9     char s[81];10     bool f;11 } word_t;12 13 word_t words[2][MAXN];14 int n[2], len[2];15 char src[2][MAXN];16 char op[5];17 18 int main() {19     int i, j, k;20     21     #ifndef ONLINE_JUDGE22         freopen("data.in", "r", stdin);23     #endif24     25     while (scanf("%s", src[0])!=EOF && (src[0][0]!=.)) {26         scanf("%s %s", op, src[1]);27         n[0] = n[1] = 0;28         for (i=0; i<2; ++i) {29             k = 0;30             for (j=1; src[i][j]; ++j) {31                 if (src[i][j]==, || src[i][j]==]) {32                     if (src[i][j-1] != [) {33                         words[i][n[i]].s[k] = \0;34                         words[i][n[i]].f = true;35                         n[i]++;36                         k = 0;37                     }38                 } else {39                     words[i][n[i]].s[k++] = src[i][j];40                 }41             }42             len[i] = j;43         }44         45         if (op[0] == +) {46             src[0][len[0]-1] = \0;47             printf("%s", src[0]);48             if (n[1] == 0) {49                 printf("]\n");50             } else {51                 if (n[0] == 0)52                     printf("%s\n", src[1]+1);53                 else54                     printf(",%s\n", src[1]+1);55             }56         } else {57             for (j=0; j<n[1]; ++j) {58                 for (i=0; i<n[0]; ++i)  {59                     if (words[0][i].f && strcmp(words[0][i].s, words[1][j].s)==0) {60                         words[0][i].f = false;61                         break;62                     }63                 }64             }65             printf("[");66             i = 0;67             while (i < n[0]) {68                 if (words[0][i].f)69                     break;70                 ++i;71             }72             if (i < n[0]) {73                 printf("%s", words[0][i].s);74                 ++i;75                 while (i < n[0]) {76                     if (words[0][i].f)77                         printf(",%s", words[0][i].s);78                     ++i;79                 }80             }81             printf("]\n");82         }83     }84     85     return 0;86 }

 

【HDOJ】3242 List Operations