Uva--502(回溯 / 模拟)

2014-07-21 19:30:21

  DEL command 

It is required to find out whether it is possible to delete given files from MS-DOS directory executing the DEL command of MS-DOS operation system only once. There are no nested subdirectories.

A note 

DEL command has the following format: DEL wildcard

The actual wildcard as well as a full file name can be made up either of a name containing 1 up to 8 characters or of a name and extension, containing up to 3 characters. The point character ``." separates the extension from the file name. The extension can be empty and this is equivalent to a name without any extension (in this case a wildcard ends with a point). In a wildcard the characters ``?" and ``*" can be used. A question mark substitutes exactly one character of the full file name excluding a point, an asterisk any sequence of characters (containing no points) even empty one. An asterisk can appear only at the last position of the name and the extension.


MS-DOS system can permit maybe other wildcards but they can not be used in this task. File names and extensions consist only of Latin capitals and digits.

Input 

The first line of the input is an integer M, then a blank line followed by M datasets. There is a blank line between datasets.

Input data for each dataset contains a list of full file names without empty lines and spaces. Each name is written in a separate line of input data file and preceded with a control sign: ``-" for delete or ``+" for keep. Full file names are not repeated. The list comprises at least one file, and at least one file is marked to be deleted. There are no more than 1000 files.

Output 

For each dataset, write to the first line of output the required DEL command (only one proposal) or IMPOSSIBLE if there is no solution. A space should separate ``DEL" from wildcard. Print a blank line between datasets.

Sample Input 

1

-BP.EXE
-BPC.EXE
+TURBO.EXE

 

Possible output 

DEL ?P*.*
思路:这题做的简直要吐血了。。。根据要删的串构造出最严格的过滤串(分成名词和后缀两部分处理,不然很容易乱)
然后用过滤串去过滤要保留的串(keep)如果仍有串不能被过滤掉,则不行(Impossible) (代码丑哭了QAQ!)
  1 /*************************************************************************
  2     > File Name: Uva502.cpp
  3     > Author: Nature
  4     > Mail: 564374850@qq.com
  5     > Created Time: Sun 20 Jul 2014 09:52:55 PM CST
  6  ************************************************************************/
  7 
  8 #include <cstdio>
  9 #include <cstring>
 10 #include <cstdlib>
 11 #include <cmath>
 12 #include <iostream>
 13 #include <algorithm>
 14 #include <queue>
 15 #include <stack>
 16 #include <set>
 17 #include <map>
 18 using namespace std;
 19 #define INF 0x3f3f3f3f
 20 #define eps 1e-8
 21 #define PI acos(-1.0)
 22 typedef long long LL;
 23 
 24 struct File{
 25     char s[30];
 26     char ex[10];
 27 }fd[1005],fk[1005];
 28 
 29 int main(){
 30     int i,j,tlen,Case,cntd,cntk;
 31     char str[30],ans[30],ans_ex[10];
 32     scanf("%d",&Case);
 33 
 34     getchar();
 35     getchar();
 36     while(Case--){
 37         cntd = 0;
 38         cntk = 0;
 39         memset(fd,0,sizeof(fd));
 40         memset(fk,0,sizeof(fk));
 41         memset(ans,0,sizeof(ans));
 42         memset(ans_ex,0,sizeof(ans_ex));
 43         //input and process
 44         //getchar();
 45         while(fgets(str,30,stdin) != NULL){
 46             if(str[0] == '\n') break;
 47             if(str[0] == '-'){
 48                 tlen = strlen(str);
 49                 for(i = 0; i < tlen; ++i){
 50                     if(str[i] == '\n'){
 51                         fd[cntd].s[i] = '\0';
 52                         break;
 53                     }
 54                     if(str[i] != '.')    fd[cntd].s[i] = str[i];
 55                     else break;
 56                 }
 57                 int tcnt = 0;
 58                 for(++i ; i < tlen; ++i)
 59                     fd[cntd].ex[tcnt++] = str[i];
 60                 fd[cntd].ex[--tcnt] = '\0';
 61                 ++cntd;
 62             }
 63             else{
 64                 tlen = strlen(str);
 65                 for(i = 0; i < tlen; ++i){
 66                     if(str[i] == '\n'){
 67                         fk[cntk].s[i] = '\0';
 68                         break;
 69                     }
 70                     if(str[i] != '.') fk[cntk].s[i] = str[i];
 71                     else break;
 72                 }
 73                 int tcnt = 0;
 74                 for(++i ; i < tlen; ++i)
 75                     fk[cntk].ex[tcnt++] = str[i];
 76                 fk[cntk].ex[--tcnt] = '\0';
 77                 ++cntk;
 78             }
 79         }
 80         //construct
 81         ans[0] = '-';
 82         for(i = 1; ; ++i){
 83             int Judge = 1,Judge2 = 0;
 84             char tem = fd[0].s[i];
 85             if(fd[0].s[i] == '\0') Judge2 = 1;
 86             for(j = 1; j < cntd; ++j){
 87                 if(fd[j].s[i] == '\0')
 88                     Judge2 = 1;
 89                 if(Judge && fd[j].s[i] != tem)
 90                     Judge = 0;
 91             }
 92             if(Judge2){
 93                 if(!Judge) ans[i] = '*';
 94                 break;
 95             }
 96             if(Judge) ans[i] = tem;
 97             else ans[i] = '?';
 98         }
 99         for(i = 0; ; ++i){
100             int Judge = 1,Judge2 = 0;
101             char tem = fd[0].ex[i];
102             if(fd[0].ex[i] == '\0') Judge2 = 1;
103             for(j = 1; j < cntd; ++j){
104                 if(fd[j].ex[i] == '\0')
105                     Judge2 = 1;
106                 if(Judge && fd[j].ex[i] != tem)
107                     Judge = 0;
108             }
109             if(Judge2){
110                 if(!Judge) ans_ex[i] = '*';
111                 break;
112             }
113             if(Judge) ans_ex[i] = tem;
114             else ans_ex[i] = '?';
115         }
116         //printf("%s.%s\n",ans,ans_ex);
117         //check
118         int Judge_ans = 1;
119         for(i = 0; i < cntk; ++i){
120             int Judge_same = 1;
121             for(j = 1; ; ++j){
122                 if(ans[j] == '\0'){
123                     if(fk[i].s[j] != '\0')
124                         Judge_same = 0;
125                     break;
126                 }
127                 if(ans[j] != fk[i].s[j]){
128                     if(fk[i].s[j] == '\0'){
129                         if(ans[j] != '*')
130                             Judge_same = 0;
131                         break;
132                     }
133                     if(ans[j] == '?');
134                     else if(ans[j] == '*')
135                         break;
136                     else{
137                         Judge_same = 0;
138                         break;
139                     }
140                 }
141             }
142             if(!Judge_same)
143                 continue; //has dif
144             for(j = 0; ; ++j){
145                 if(ans_ex[j] == '\0'){
146                     if(fk[i].ex[j] != '\0')
147                         Judge_same = 0;
148                     break;
149                 }
150                 if(ans_ex[j] != fk[i].ex[j]){
151                     if(fk[i].ex[j] == '\0'){
152                         if(ans_ex[j] != '*')
153                             Judge_same = 0;
154                         break;
155                     }
156                     if(ans_ex[j] == '?');
157                     else if(ans_ex[j] == '*')
158                         break;
159                     else{
160                         Judge_same = 0;
161                         break;
162                     }
163                 }
164             }
165             if(Judge_same){
166                 Judge_ans = 0;
167                 break;
168             }
169         }
170         if(Judge_ans)
171             printf("DEL %s.%s\n",ans + 1,ans_ex);
172         else
173             printf("IMPOSSIBLE\n");
174         if(Case)
175             printf("\n");
176     }
177     return 0;
178 }

 



posted @ 2014-07-21 19:34  Naturain  阅读(176)  评论(0编辑  收藏  举报