Magic Number(dp)

Magic Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1057    Accepted Submission(s): 444


Problem Description
There are many magic numbers whose lengths are less than 10. Given some queries, each contains a single number, if the Levenshtein distance (see below) between the number in the query and a magic number is no more than a threshold, we call the magic number is the lucky number for that query. Could you find out how many luck numbers are there for each query?

Levenshtein distance (from Wikipedia http://en.wikipedia.org/wiki/Levenshtein_distance):
In information theory and computer science, the Levenshtein distance is a string metric for measuring the amount of difference between two sequences. The term edit distance is often used to refer specifically to Levenshtein distance.
The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character. It is named after Vladimir Levenshtein, who considered this distance in 1965.
For example, the Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:
1.kitten → sitten (substitution of 's' for 'k')
2.sitten → sittin (substitution of 'i' for 'e')
3.sittin → sitting (insertion of 'g' at the end).
 

Input
There are several test cases. The first line contains a single number T shows that there are T cases. For each test case, there are 2 numbers in the first line: n (n <= 1500) m (m <= 1000) where n is the number of magic numbers and m is the number of queries.
In the next n lines, each line has a magic number. You can assume that each magic number is distinctive.
In the next m lines, each line has a query and a threshold. The length of each query is no more than 10 and the threshold is no more than 3.
 

Output
For each test case, the first line is "Case #id:", where id is the case number. Then output m lines. For each line, there is a number shows the answer of the corresponding query.
 

Sample Input
  
1 5 2 656 67 9313 1178 38 87 1 9509 1
 

Sample Output
  
Case #1: 1 0
 

Author
BJTU
 

Source
 

Recommend
zhoujiaqi2010
 

Statistic | Submit | Discuss | Note


参考资料:点击打开链接

按照参考资料上的方法做会超时,如果设一个计数器记录修改字符串的次数,当次数大于threshold时立即返回就不会超时,但是时间也要500+,不过这个方法倒是非常简而易行,不用花太多脑筋。

AC Code:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

char ch[1501][11], q[11];
int th, i;

int min(int a, int b, int c)
{
    if(a > b) a = b;
    if(a > c) a = c;
    return a;
}

int lev(int lena, int lenb, int cnt)
{
    if(cnt > th) return cnt;
    int cost = 0;
    if(ch[i][lena - 1] != q[lenb - 1]) cost = 1;
    if(lena == 0) return lenb;
    if(lenb == 0) return lena;
    return min(lev(lena-1, lenb, cnt+cost)+1, lev(lena, lenb-1, cnt+cost)+1, lev(lena-1, lenb-1, cnt+cost)+cost);
}

int main()
{
    int n, m, t, id = 0, cnt;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &m);
        for(i = 0; i < n; ++i)
        {
            scanf("%s", ch[i]);
        }
        printf("Case #%d:\n", ++id);
        while(m--)
        {
            cnt = 0;
            scanf("%s %d", q, &th);
            for(i = 0; i < n; ++i)
            {
                if(lev(strlen(ch[i]), strlen(q), 0) <= th) cnt++;
            }
            printf("%d\n", cnt);
        }
    }
    return 0;
}


### 解析无效魔数问题 当遇到 `Invalid magic number` 错误时,通常意味着系统尝试加载一个不兼容或损坏的文件。对于Linux启动过程中出现的此类错误,可能的原因包括但不限于引导装载程序配置不当、内核镜像损坏或是分区表存在问题。 针对具体场景中的 `"Invalid magic number 0 error 13: invalid or unsupported executable format"` 报错,在Oracle Linux环境中,这可能是由于GRUB引导记录被破坏或者是新安装的操作系统版本与现有硬件环境存在兼容性问题所引起的[^1]。 为了修复这个问题,可以采取如下措施: #### 方法一:通过Live CD/USB恢复 如果能够访问到系统的物理介质,则建议使用官方提供的救援模式光盘或者U盘来解决问题。进入救援模式后,重新安装grub2可以帮助恢复正常启动流程: ```bash mount /dev/sdaX /mnt/sysimage # 将根文件系统挂载至/mnt/sysimage下 chroot /mnt/sysimage # 切换到目标系统环境下执行命令 grub2-install /dev/sda # 安装新的bootloader到磁盘上 update-grub # 更新grub菜单列表 exit # 返回原shell会话 reboot # 重启计算机测试效果 ``` 这里假设 `/dev/sda` 是主硬盘设备名,而 `/dev/sdaX` 表示实际使用的根分区编号,请根据实际情况调整上述指令中的参数设置[^3]。 #### 方法二:检查并修正MBR/GPT数据结构 有时该类错误也可能是由MBR(Master Boot Record)或GPT(GUID Partition Table)遭到意外修改造成的。此时可借助工具如TestDisk来进行检测和修复工作。这类操作较为复杂且风险较高,因此除非非常熟悉相关过程,否则应谨慎行事[^4]。 #### Python编译缓存引发的问题 值得注意的是,“bad magic number” 还经常出现在Python上下文中,特别是涉及到`.pyc` 文件的时候。不过这种情况显然不属于当前讨论范围内的操作系统级别启动失败情况;而是指解释器试图运行不同版本之间互不匹配的字节码文件所致[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值