DP:magic number

本文介绍了一种利用Levenshtein距离算法解决特定查询问题的方法,该算法用于衡量两个字符串之间的相似度,通过计算最少编辑距离来找出查询字符串与预设魔法数字间的最相近匹配。

Magic Number

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


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


参考wikipedia

int LevenshteinDistance(char s[1..m], char t[1..n])
{
  // for all i and j, d[i,j] will hold the Levenshtein distance between
  // the first i characters of s and the first j characters of t;
  // note that d has (m+1)x(n+1) values
  declare int d[0..m, 0..n]

  clear all elements in d  // set each element to zero

  // source prefixes can be transformed into empty string by
  // dropping all characters 
  for i from 1 to m
  {
    d[i, 0] := i                    
  }

  // target prefixes can be reached from empty source prefix
  // by inserting every characters
  for j from 1 to n
  {
    d[0, j] := j                    
  }

  for j from 1 to n
  {
    for i from 1 to m
    {
      if s[i] = t[j] then  
        d[i, j] := d[i-1, j-1]       // no operation required
      else
        d[i, j] := minimum
                   (
                     d[i-1, j] + 1,  // a deletion
                     d[i, j-1] + 1,  // an insertion
                     d[i-1, j-1] + 1 // a substitution
                   )
    }
  }

  return d[m,n]
}

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

using namespace std;

char mg[1510][12];
char qu[1010][12];
int th[1010];
int n, m;
int mgl[1510];
int qul[1010];
int ans[1010];

int levenDistance(char a[], char b[]){
    int d[12][12];
    int i, j;
    int la = strlen(a);
    int lb = strlen(b);
    memset(d, 0, sizeof(d));
    for(i = 0; i <= la; i ++)
        d[i][0] = i;
    for(j = 0; j <= lb; j ++)
        d[0][j] = j;
    for(j = 1; j <= lb; j ++)
        for(i = 1; i <= la; i ++){
            if(a[i] == b[j]){
                d[i][j] = d[i-1][j-1];
            }
            else{
                d[i][j] = min(d[i-1][j]+1, min(d[i][j-1]+1, d[i-1][j-1]+1));
            }
        }
    //printf("%s %s\n", a, b);
    //printf("%d\n", d[la][lb]);
    return d[la][lb];
}

int main(){
    int t;
    int i, j;
    int cnt = 0;
    scanf("%d", &t);
    while(t --){
        cnt ++;
        char ss[12];
        memset(ans, 0, sizeof(ans));
        scanf("%d %d", &n, &m);
        for(i = 0; i < n; i ++){
            scanf("%s", ss);
            mg[i][0] = 's';
            mg[i][1] = '\0';
            strcat(mg[i], ss);
            mgl[i] = strlen(mg[i]);
        }
        for(i = 0; i < m; i ++){
            scanf("%s", ss);
            qu[i][0] = 's';
            qu[i][1] = '\0';
            strcat(qu[i], ss);
            qul[i] = strlen(qu[i]);
            scanf("%d", &th[i]);
        }
        for(i = 0; i < m; i ++){
            for(j = 0; j < n; j ++){
                if(abs(qul[i] - mgl[j]) <= th[i]){
                    if(levenDistance(mg[j], qu[i]) <= th[i])
                        ans[i] ++;
                }
            }
        }
        printf("Case #%d:\n", cnt);
        for(i = 0; i < m; i ++)
            printf("%d\n", ans[i]);
    }
    return 0;
}


private void initViews() { binding.update.setVisibility(VISIBLE); binding.getRoot().setOnTouchListener((v, event) -> { // 返回true表示消费所有事件,不再向下传递 return true; }); int type = -1; if (fragmentIntent != null) { type = fragmentIntent.getInt(UpdateLaunchFlag.LaunchFlag); } Logger.d("UPDATE", "type" + type); if (type != -1) { binding.youshujukegengxin.setVisibility(View.GONE); binding.noupdate.setVisibility(View.GONE); binding.chakan.setVisibility(View.GONE); binding.quxiao.setVisibility(View.GONE); binding.iknow.setVisibility(View.GONE); binding.nospace.setVisibility(View.GONE); binding.cancel.setVisibility(View.GONE); switch (type) { case UpdateLaunchFlag.UPDATE: binding.chakan.setVisibility(VISIBLE); binding.quxiao.setVisibility(VISIBLE); binding.youshujukegengxin.setVisibility(VISIBLE); break; case UpdateLaunchFlag.NOUPDATE: binding.iknow.setVisibility(VISIBLE); binding.noupdate.setVisibility(VISIBLE); break; default: break; } } 给出详细的注释,根据:<!-- 保留 layout 标签但不声明 data --> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/update" android:layout_width="720dp" android:layout_height="386dp" android:layout_marginLeft="600dp" android:layout_marginTop="291dp" android:background="#FFFFFF"> <!--提示--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/tishi" android:layout_width="600dp" android:layout_height="56dp" android:layout_marginLeft="60dp" android:layout_marginTop="40dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--提示框--> <TextView android:id="@+id/tishikuang" android:layout_width="600dp" android:layout_height="56dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#000000" android:textSize="36sp" android:text="@string/update_tips_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/tishi" app:layout_constraintTop_toTopOf="@+id/tishi" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--有数据可更新--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/youshujukegengxin" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/youshujukegengxintext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_updatable_data_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/youshujukegengxin" app:layout_constraintTop_toTopOf="@+id/youshujukegengxin" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--查看--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/chakan" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="60dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/chakantext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_watch_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#B87333" app:layout_constraintStart_toStartOf="@+id/chakan" app:layout_constraintTop_toTopOf="@+id/chakan" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--取消--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/quxiao" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="380dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/quxiaotext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_cancel_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#818181" app:layout_constraintStart_toStartOf="@+id/quxiao" app:layout_constraintTop_toTopOf="@+id/quxiao" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--没有数据可更新--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/noupdate" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--没有数据可提示文言--> <TextView android:id="@+id/noupdatetext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_no_updatable_data_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/noupdate" app:layout_constraintTop_toTopOf="@+id/noupdate" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--我知道了--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/iknow" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="220dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--我知道了文言--> <TextView android:id="@+id/iknowtext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_konwed_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#818181" app:layout_constraintStart_toStartOf="@+id/iknow" app:layout_constraintTop_toTopOf="@+id/iknow" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--存储空间不足--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/nospace" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/nospacetext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_no_space_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/nospace" app:layout_constraintTop_toTopOf="@+id/nospace" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--是否结束数据更新--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/cancel" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/canceltext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_end_update_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/cancel" app:layout_constraintTop_toTopOf="@+id/cancel" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout> </layout>
09-09
c++:Problem Statement Takahashi is about to play a game where he fights N monsters in order. Initially, Takahashi's health is H and his magic power is M. For the i-th monster he fights, he can choose one of the following actions: Fight without using magic. This can only be chosen when his health is at least A i ​ , and his health decreases by A i ​ and the monster is defeated. Fight using magic. This can only be chosen when his magic power is at least B i ​ , and his magic power decreases by B i ​ and the monster is defeated. The game ends when all N monsters are defeated or when he cannot take any action. What is the maximum number of monsters he can defeat before the game ends? Constraints 1≤N≤3000 1≤H,M≤3000 1≤A i ​ ,B i ​ ≤3000 All input values are integers. Input The input is given from Standard Input in the following format: N H M A 1 ​ B 1 ​ A 2 ​ B 2 ​ ⋮ A N ​ B N ​ Output Output the answer. Sample Input 1 Copy 4 10 14 5 8 5 6 7 9 99 99 Sample Output 1 Copy 3 By taking the following actions, Takahashi can defeat 3 monsters before the game ends. Initially, his health is 10 and his magic power is 14. Fight the 1st monster without using magic. His health decreases by 5, becoming health 5 and magic power 14. Fight the 2nd monster without using magic. His health decreases by 5, becoming health 0 and magic power 14. Fight the 3rd monster using magic. His magic power decreases by 9, becoming health 0 and magic power 5. For the 4th monster, he cannot choose either action, so the game ends. Sample Input 2 Copy 3 3000 3000 3 3 3 3 3 3 Sample Output 2 Copy 3 He may be able to defeat all monsters. Sample Input 3 Copy 10 8 8 2 2 2 3 2 2 1 2 2 3 1 2 3 3 3 2 3 1 3 2 Sample Output 3 Copy 9
06-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值