首页 > 试题广场 >

配置文件恢复

[编程题]配置文件恢复
  • 热度指数:110287 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

有6条配置命令,它们执行的结果分别是:

命   令 执   行
reset reset what
reset board board fault
board add where to add
board delete no board at all
reboot backplane impossible
backplane abort install first
he he unknown command

注意:he he不是命令。

为了简化输入,方便用户,以“最短唯一匹配原则”匹配(注:需从首字母开始进行匹配):

1、若只输入一字串,则只匹配一个关键字的命令行。例如输入:r,根据该规则,匹配命令reset,执行结果为:reset what;输入:res,根据该规则,匹配命令reset,执行结果为:reset what;
2、若只输入一字串,但匹配命令有两个关键字,则匹配失败。例如输入:reb,可以找到命令reboot backpalne,但是该命令有两个关键词,所有匹配失败,执行结果为:unknown command

3、若输入两字串,则先匹配第一关键字,如果有匹配,继续匹配第二关键字,如果仍不唯一,匹配失败。
例如输入:r b,找到匹配命令reset board 和 reboot backplane,执行结果为:unknown command。
例如输入:b a,无法确定是命令board add还是backplane abort,匹配失败。

4、若输入两字串,则先匹配第一关键字,如果有匹配,继续匹配第二关键字,如果唯一,匹配成功。例如输入:bo a,确定是命令board add,匹配成功。
5、若输入两字串,第一关键字匹配成功,则匹配第二关键字,若无匹配,失败。例如输入:b addr,无法匹配到相应的命令,所以执行结果为:unknow command。
6、若匹配失败,打印“unknown command”

注意:有多组输入。
数据范围:数据组数:,字符串长度
进阶:时间复杂度:,空间复杂度:

输入描述:

多行字符串,每行字符串一条命令



输出描述:

执行结果,每条命令输出一行

示例1

输入

reset
reset board
board add
board delet
reboot backplane
backplane abort

输出

reset what
board fault
where to add
no board at all
impossible
install first
#HJ66 配置文件恢复

while True:
    try:
        cmd=input().strip().split(' ')#输入命令,去掉前后空格,split()分割成列表

        #定义命令列表
        key=["reset board","board add","board delete","reboot backplane","backplane abort"]
        value=["board fault","where to add","no board at all","impossible","install first"]


        if len(cmd)<1 or len(cmd)>2:
            print("unknown command")
        elif len(cmd)==1:#只有一个字符串的时候,只需要判断是否是reset命令
            if cmd[0]=="reset"[:len(cmd[0])]:#判断是否是reset命令,从reset切片长度相同校验是否一致
                print("reset what")
            else:
                print("unknown command")
        else:#有两个字符串的时候
            #先校验第一个字符串是否在key列表第一个字符串中
            #再校验第二个字符串是否在key列表第二个字符串中且唯一
            index=0#记录key列表的索引
            num=0#记录key列表中相同字符串的个数
            for i in range(len(key)):
                a=key[i].split()#分割key列表的字符串
                if cmd[0]==a[0][:len(cmd[0])] and cmd[1]==a[1][:len(cmd[1])]:
                    num+=1
                    index=i
            if num==1:
                print(value[index])
            else:
                print("unknown command")


    except:
        break

发表于 2024-11-27 09:31:15 回复(0)
while True:
    try:
        a = input().split()
        b = {'reset': 'reset what', 'reset board': 'board fault', 'board add': 'where to add',
             'board delete': 'no board at all', 'reboot backplane': 'impossible',
             'backplane abort': 'install first', 'he he': 'unknown command'}
        d = []
        for k in b.keys():
            c = k.split()
            if len(a) == 1 and len(c) == 1 and a[0] in c[0]:
                d.append(b[k])
            elif len(a) > 1 and len(c) > 1 and c[0].startswith(a[0]) and c[1].startswith(a[1]):
                d.append(b[k])
        if len(d) == 1:
            print(d[0])
        else:
            print('unknown command')
    except:
        break

发表于 2023-04-28 17:43:53 回复(0)
import sys

def identify(order):
    orders={'reset':['','board'],'board':['add','delete'],'reboot':['backplane'],'backplane':['abort']}
    res=[[],[]]
    for i in list(orders.keys()):
        if i.find(order[0])==0:
            res[0].append(i)
    if not res[0]:
        return '' 
    part=res[0][0]
    if len(order)==1:
        res[1].append('')
    else:
        for s in res[0]:
            for i in list(orders[s]):
                if i.find(order[1])==0:
                    res[1].append(i)
                    part=s
    if len(res[1])!=1:
        return ''
    return part+' '+res[1][0]

def excute(order):
    orders={'reset':'reset what','reset board':'board fault','board add':'where to add','board delete':'no board at all','reboot backplane':'impossible','backplane abort':'install first'}
    return orders[order] if orders.__contains__(order) else 'unknown command'

for line in sys.stdin:
    text = line.strip().split(' ')
    order = identify(text)
    print(excute(order.strip()))
    

发表于 2023-04-27 17:50:45 回复(0)
if __name__ == "__main__":
    while True:
        try:
            dic ={'reset': 'reset what', 'reset board': 'board fault', 'board add':'where to add',
            'board delete':'no board at all', 'reboot backplane':'impossible', 'backplane abort':'install first'}
            twoWords = [['reset','board'], ['board','add'], ['board', 'delete'], ['reboot', 'backplane'],['backplane', 'abort']]
            l = list(map(str, input().split()))
            if len(l) == 1 and l[0] == 'reset'[:len(l[0])]:
                print(dic['reset'])
            elif len(l) == 2:
                count = 0
                res = ""
                for x in twoWords:
                    if l[0] == x[0][:len(l[0])] and l[1] == x[1][:len(l[1])]:
                        count += 1
                        res = x[0] + ' '+ x[1]
                if count == 1:
                    print(dic[res])
                else:
                    print("unknown command")
            else:
                print("unknown command")
        except:
            break

发表于 2023-02-28 00:01:59 回复(0)
a = 'reset'
a1 = 'reset what'
b = 'reset board'
b1 = 'board fault'
c = 'board add'
c1 = 'where to add'
d = 'board delete'
d1 = 'no board at all'
e = 'reboot backplane'
e1 = 'impossible'
f = 'backplane abort'
f1 = 'install first'
g = 'unknown command'
l = [a,b,c,d,e,f]
l1 = [a1,b1,c1,d1,e1,f1]

while True:
    try:
        result = []
        s = input().split()
        if len(s) == 1:
            if str(s[0]) in a:
                print(a1)
        else:
            for i in range(1, len(l)):
                p = l[i].split()[0]
                o = l[i].split()[1]
                p1 = l[i].split()[0][0:len(s[0])]
                p2 = l[i].split()[1][0:len(s[1])]
                if s[0] == l[i].split()[0][0:len(s[0])] and s[1] in l[i].split()[1][0:len(s[1])]:
                    result.append(l[i])
            if len(result) == 1:
                print(l1[l.index(result[0])])
            else:
                print(g)
    except:
        break

10/11组用例通过,能帮忙看一下那不对吗?
发表于 2023-01-16 13:33:44 回复(0)
import re

command = {
    "reset": "reset what",
    "reset board": "board fault",
    "board add": "where to add",
    "board delete": "no board at all",
    "reboot backplane": "impossible",
    "backplane abort": "install first",
}
while True:
    try:
        cmd = input().split()
        match_cmd = ''
        if len(cmd) == 1:
            temp = '^'+''.join(cmd)+ '.*'
            for i in command.keys():
                match = re.match(temp, i)
                if match and ' ' not in match.group():
                    match_cmd += match.group()
        else:
            temp = '^'+'.*[ ]'.join(cmd)+'.*'
            for i in command.keys():
                match = re.match(temp, i)
                if match:
                    match_cmd += match.group()
        try:
            print(command[match_cmd])
        except:
            print('unknown command') 
    except:
        break

发表于 2022-11-06 02:31:02 回复(0)
one = set(['reset','reboot','board','backplane'])
orders={}
orders['r'] = ['reset board','reboot backplane']
orders['b'] = ['board add','board delete','backplane abort']
motion = {'reset':'reset what','reset board':'board fault','reboot backplane':'impossible',
         'board add':'where to add','board delete':'no board at all',
          'backplane abort':'install first'}
while True:
    try:
        s = input().split(' ')
        n = len(s)
        if n == 1:
            if s[0][0]=='r' and s[0] in 'reset':
                print(motion['reset'])
                continue
            else:
                print('unknown command')
        else:
            if s[0][0] == 'r'&nbs***bsp;s[0][0] == 'b':
                count = []
                a = s[0][0]
                for order in orders[a]:
                    tmp = order.split(" ")
                    if s[0] in tmp[0] and s[1] in tmp[1] and s[1][0]==tmp[1][0]:
                        count.append(order)
                if len(count) != 1:
                    print('unknown command')
                else:
                    print(motion[count[0]])
            else:
                print('unknown command')
    except:
        break

发表于 2022-08-27 11:21:19 回复(0)
while True:
    try:
        dic = {
            'reset'           : 'reset what',
            'reset board'     : 'board fault',
            'board add'       : 'where to add',
            'board delete'    : 'no board at all',
            'reboot backplane': 'impossible',
            'backplane abort' : 'install first'}
        cmd = input().split()
        n = len(cmd)
        if n > 2&nbs***bsp;n == 0:
            print('unknown command')
        elif n == 1:
            # 注意输入后split()转为了list,所以此处为cmd[0]
            if 'reset'.startswith(cmd[0]): 
                print('reset what')
            else:
                print('unknown command')
        elif n == 2:
            dic.pop('reset')
            cnt = 0;
            for key in dic.keys():
                subkey = key.split()
                # 如果匹配到命令,且唯一
                if subkey[0].startswith(cmd[0]) and subkey[1].startswith(cmd[1]):
                    cnt += 1
                    tmpkey = key
            if cnt == 1:
                print(dic[tmpkey])
            else:
                print('unknown command')  
    except:
        break

发表于 2022-07-28 15:38:21 回复(0)
key = ['reset',
       'reset board',
       'board add',
       'board delete',
       'reboot backplane',
       'backplaneabort']
value = ['reset what',
         'board fault',
         'where to add',
         'no board at all',
         'impossible',
         'install first']
while True:
    try:
        s = input().split(' ')
        l = len(s)
        if l == 1 and s[0] in ['r','re','res','rese','reset']:
            print(value[0])
        # 两种特殊情况先行考虑
        elif l == 2 and s[0] == 'r' and s[1] == 'b':
            print('unknown command')
        elif l == 2 and s[0] == 'b' and s[1] == 'a':
            print('unknown command')
        elif l == 2:
            if s[0] in ['r','re','res','rese','reset'] and s[1] in ['b','bo','boa','boar','board']:
                print(value[1])
            elif s[0] in ['b','bo','boa','boar','board'] and s[1] in ['a','ad','add']:
                print(value[2])
            elif s[0] in ['b','bo','boa','boar','board'] and s[1] in ['d','de','del','dele','delet','delete']:
                print(value[3])
            elif s[0] in ['r','re','reb','rebo','reboo','reboot'] and s[1] in ['b','ba','bac','back','backp','backpl',
                                                                               'backpla','backplan','backplane']:
                print(value[4])
            elif s[0] in ['b','ba','bac','back','backp','backpl','backpla','backplan','backplane'] and s[1] in ['a',
                                                                                                                'ab',
                                                                                                                'abo',
                                                                                                                'abor',
                                                                                                                'abort']:
                print(value[5])
            else:
                print('unknown command')
        else:
            print('unknown command')
    except:
        break

发表于 2022-07-04 11:50:47 回复(0)
while True:
    try:
        m=input().strip().split()
        key=["reset","reset board","board add","board delete","reboot backplane","backplane abort"]
        value=["reset what","board fault","where to add","no board at all","impossible","install first"]
        if len(m)<1&nbs***bsp;len(m)>2:
            print("unknown command")
        elif len(m)==1:  
            #当输入一个字符串
            m_out = m[0]
            if m_out ==key[0][:len(m_out)]:
                print(value[0])
            else:
                print("unknown command")
        else:
            m1, m2 = m
            index=[]
            for i in range(1,len(key)):
                a1, a2 =key[i].split()
                if m1==a1[:len(m1)] and m2 == a2[:len(m2)]:
                    index.append(i)

            if len(index)!=1:
                print("unknown command")
            else:
                print(value[index[0]])
    except:
        break

发表于 2022-06-03 14:14:07 回复(0)
while True:
    try:
        s1 = input().split()
        dict1 = {'reset':'reset what','reset board':'board fault',
                 'board add':'where to add','board delete':'no board at all',
                 'reboot backplane':'impossible','backplane abort':'install first'}
        value1 = []         # 获取匹配到的value值
        for key, value in dict1.items():
            keys = key.split()  # key单词列表
            if len(s1) == 1:   #输入单词量为1
                if len(keys)==1 and (s1[0] == keys[0][:len(s1[0])]):  #key单词量为1,且s1[0]等于keys[0]中切片
                    value1.append(value)
            elif len(s1) == 2:
                if len(keys)==2 and (s1[0] == keys[0][:len(s1[0])]) and (s1[1] == keys[1][:len(s1[1])]) :
                    value1.append(value)
        if len(value1) == 1:    # 匹配到的数量为1,输出,否则不是唯一匹配
            print(value1[0])
        else:
            print('unknown command')               
    except:
        break
发表于 2022-05-17 11:56:20 回复(0)
import re
while 1:
    try:
        a=input().split()
        ls=['reset','reset board','board add','board delete','reboot backplane','backplane abort']
        ll=['reset what','board fault','where to add','no board at all','impossible','install first']  
        if len(a)==1:
            if re.match(a[0],'reset'):
                print('reset what')
            else:
                print('unknown command')
        if len(a)==2:
            u=0
            v=0
            for j,k in enumerate(ls):
                l2=k.split()
                if re.match(a[0],l2[0]) and re.match(a[-1],l2[-1]):
                    u+=1
                    v=j
            if u==1:
                print(ll[v])
            else:
                print('unknown command')            
    except:
        break
            
发表于 2022-05-08 14:35:14 回复(0)
正则表达式是真的好用呀
one={'reset':'reset what'}
two={'reset board':'board fault',
'board add':'where to add',
'board delete':'no board at all',
'reboot backplane':'impossible',
'backplane abort':'install first'}
import re
def ab1(s):
    if re.search('^'+s, 'reset'):
        return one['reset']
    else:
        return 'unknown command'
def ab2(s):
    ls=s.split()
    s=r'^'+ls[0]+'\w*\s'+ls[1]+'\w*'
    count=0
    for i in list(two.keys()):
        if re.search(s,i):
            count+=1
            command=two[i]
    if count==1:
        return command
    else:
        return 'unknown command'
while True:
    try:
        s=input()
        sig=len(s.split())
        if sig==1:
            print(ab1(s))
        elif sig==2:
            print(ab2(s))
        else:
            print('unknown command')
    except:
        break

发表于 2022-05-07 18:31:31 回复(0)
a=[]
for i in range(0,7):
    a.append([])
a[1].append('reset')
a[1].append('reset what')
a[2].append('reset board'.split())
a[2].append('board fault')
a[3].append('board add'.split())
a[3].append('where to add')
a[4].append('board delete'.split())
a[4].append('no board at all')
a[5].append('reboot backplane'.split())
a[5].append('impossible')
a[6].append('backplane abort'.split())
a[6].append('install first')
un='unknown command'
mingling=[]
while True:
    try:
        mingling=input().split()
        l=len(mingling)
        if l==1:
            if mingling[0]==a[1][0][0:len(mingling[0])]:
                print(a[1][1])
                continue
            else:
                print(un)
                continue
        p=[]
        for i in range(2,7):
            if mingling[0]==a[i][0][0][0:len(mingling[0])] and mingling[-1]==a[i][0][1][0:len(mingling[1])]:
                p.append(a[i][1])
        if len(p)==1:
            print(p[0])
        else:
            print(un)
    except:
        break
发表于 2022-04-25 23:32:43 回复(0)
题目不难 但是每次做题调试都花好多时间 都是一些小问题 难受
import re
while True:
    try:
        s=input().strip().split()
        command=['reset','reset board','board add','board delete','reboot backplane','backplane abort']
        exe=['reset what','board fault','where to add','no board at all','impossible','install first']
    
        if len(s)==1:
            if re.search('^'+s[0],'reset'):
                print("reset what")
            else:
                print("unknown command")
        elif len(s)==2:
            count=0
            re_res=""
            for i in range(1,len(command)):
                first,seconde=command[i].split()
                if re.search('^'+s[0],first) and re.search('^'+s[1],seconde):
                    count+=1
                    re_res=exe[i]
            if count == 1:
                print(re_res)
            else:
                print("unknown command")
        else:
            print("unknown command")
                        
                    
    except:
        break

发表于 2022-04-25 17:59:27 回复(0)
f1 = ['reset','reset','board','board','reboot', 'backplane']
s2 = ['1','board','add','delete','backplane','abort']
ml = ['reset what','board fault','where to add','no board at all','impossible','install first','unknown command']
while 1:
    try:
        s = input().split()
        if len(s) == 1:
            s.append('1')
        flag = []
        res = -1
        for i in range(6):
            if s[0] in f1[i][0:len(s[0])] and s[1] in s2[i][0:len(s[1])]:
                flag.append(i)
        if len(flag) ==1:
            res = flag[0]
        print(ml[res])     
        pass
    except:
        break

发表于 2022-04-12 22:52:35 回复(0)
d={'reset board':'board fault',
   'board add':'where to add',
   'board delete':'no board at all',
   'reboot backplane':'impossible',
   'backplane abort':'install first'}

while True:
	try:
		arr=input().split()
		if len(arr)==1 and 'reset'.startswith(arr[0]):
			print('reset what')
		elif len(arr)==2:
			res=[]
			for i in d.keys():
				if i.split()[0].startswith(arr[0]) and i.split()[1].startswith(arr[1]):
					res.append(d[i])
			if len(res)==1:
				print(*res)
			else:
				print('unknown command')
		else:
			print('unknown command')
	except:
		break

发表于 2022-03-03 13:37:53 回复(0)