990人阅读 评论(0)

# 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) orIMPOSSIBLE 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*.*

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
#include <algorithm>
using namespace std;
char substr[1000+5][15+5]={'\0'};
int subcnt=0;
int M=0;
char delstr[15+5]={'\0'};//delete string
char instr[15+5]={'\0'};//input string
/*struct the delstr*/
int FindDelStr()
{
char prestr[15]={'\0'};//compare string
char rearstr[15]={'\0'};
int i1=0,i2=0,j=0,k=0;
if(subcnt>=1)
{
strcpy(prestr,substr[0]);
strcpy(delstr,prestr);
}
for(k=1;k<=subcnt-1;k++)
{
strcpy(rearstr,substr[k]);
//get the delstr pattern
i1=0;
i2=0;
j=0;
while(prestr[i1]!='.'&&rearstr[i2]!='.')//~~~~~.  left of the '.'
{
if(prestr[i1]==rearstr[i2])
{
delstr[j]=prestr[i1];
}
else
{
if(prestr[i1]=='*')
{
//let the i2 point the left 1 bit of the '.'
while(rearstr[i2]!='.')
{
i2++;
}
i2--;//point the left 1 bit of the '.'
delstr[j]='*';
}
else
{
delstr[j]='?';
}
}
i1++;
i2++;
j++;
}
if(prestr[i1]==rearstr[i2])//=='.' besides  i1==i2
{
delstr[j]='.';
j++;
}
else
{
delstr[j]='*';
j++;
delstr[j]='.';
j++;
}
while(prestr[i1]!='.')
{
i1++;
}
i1++;//i1 point the . right
while(rearstr[i2]!='.')
{
i2++;
}
i2++;//i2 point the . right

while(prestr[i1]!='\0'&&rearstr[i2]!='\0')//~~~~~.  left of the '.'
{
if(prestr[i1]==rearstr[i2])
{
delstr[j]=prestr[i1];
}
else
{
if(prestr[i1]=='*')
{
//let the i2 point the left 1 bit of the '\0'
while(rearstr[i2]!='\0')
{
i2++;
}
i2--;//point the left 1 bit of the '\0'
delstr[j]='*';
}
else
{
delstr[j]='?';
}
}
i1++;
i2++;
j++;
}
if(prestr[i1]==rearstr[i2])//=='.' besides  i1==i2
{
delstr[j]='\0';
j++;
}
else
{
delstr[j]='*';
j++;
delstr[j]='\0';
j++;
}
strcpy(prestr,delstr);//next update the delstr
}
return(0);
}
/*test the delstr whether can test the no del string*/
bool TestNoDelStr()
{
{
strcpy(delstr,"*.*");
return(true);
}
int i=0,j=0,k=0;
int flag=0;//flag=1 indicate it is match for the delstr
{
i=0;
j=0;
flag=1;
{
{
if(delstr[i]=='?')
{

}
else if(delstr[i]=='*')
{
while(addstr[k][j]!='.')//let the point the '.' left one bit
{
j++;
}
j--;
}
else
{
flag=0;
//return(true);
}
}
i++;
j++;
}
if(delstr[i]=='*')
{
while(addstr[k][j]!='.')//let the point the '.' left one bit
{
j++;
}
j--;
i++;
j++;
}
if(delstr[i]!=addstr[k][j])//must reach '.' at the same times
{
flag=0;
//return(true);
}
i++;
j++;//point the . right
{
{
if(delstr[i]=='?')
{

}
else if(delstr[i]=='*')
{
while(addstr[k][j]!='\0')//let the point the '.' left one bit
{
j++;
}
j--;
}
else
{
flag=0;
//return(true);
}
}
i++;
j++;
}
if(delstr[i]=='*')
{
while(addstr[k][j]!='\0')//let the point the '.' left one bit
{
j++;
}
j--;
i++;
j++;
}
if(delstr[i]!=addstr[k][j])//must reach '\0' at the same times
{
flag=0;
//return(true);
}
if(flag)
{
return(false);
}
}
return(true);//the delstr match the all addstr then return false that this delstr is invalid
}
/*for test*/
int test()
{
return(0);
}
/*main process*/
int MainProc()
{
scanf("%d",&M);
getchar();//eat the \n
getchar();
while(M--)
{
//init
subcnt=0;
memset(delstr,'\0',sizeof(delstr));
while(fgets(instr,15,stdin)!=NULL&&instr[0]!='\n')//not the EOF,EOF is int type
{
int inlen=strlen(instr);
instr[inlen-1]='\0';//cover the \n
inlen--;
if(strchr(instr,'.')==NULL)
{
instr[inlen]='.';
instr[inlen+1]='\0';
inlen++;
}
if(instr[0]=='-')//- delete string
{
strcpy(substr[subcnt],instr+1);//abandon the + or -
subcnt++;
}
else//+ do not delete string
{
}
}
//test
int i=0;
FindDelStr();//if addcnt==0  we can del *.*
if(TestNoDelStr())//the delstr is valid
{
printf("DEL %s\n",delstr );
}
else//impossible
{
printf("IMPOSSIBLE\n");
}
//blank line between datasets
if(M>0)
{
printf("\n");
}
}
return(0);
}
int main(int argc, char const *argv[])
{
/* code */
MainProc();
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：545773次
• 积分：5920
• 等级：
• 排名：第5022名
• 原创：145篇
• 转载：54篇
• 译文：0篇
• 评论：98条
阅读排行
评论排行
最新评论