链接:https://ac.nowcoder.com/acm/contest/3665/E
来源:牛客网

题目描述

Morse code is a character encoding scheme used in telecommunication that encodes text characters as standardized sequences of two different signal durations called dots and dashes or dits and dahs. Morse code is named for Samuel F. B. Morse, an inventor of the telegraph.

Space is used to split Morse code, but if all spaces are lost, then the Morse code may have many meanings.For example: ".-. "can be translated to "AE"or "EN"or "R", but the string translated from Morse code can’t contain ’E’ and ’T’. So "R"is the longest string it can represent. Now hery is given a Morse code, he wants to know the length of the longest string it can represent.

输入描述:

The first line is an integer T, the number of test cases.
Next T lines, each line contains a Morse code consist of ’.’ and ’-’.
(1 ≤ T ≤ 100, the number of characters input will not exceed 10^6).

输出描述:

For each Morse code ,output the length of the longest string it can represent.

示例1

输入

  1. ..--.-.
  2. --.-.--

输出

  1.  

长度为2的4种情况已经全部出现过,长度为3的8种情况也已经出现过,因此我们一定能够利用长度为2或3的字母去填充,因此答案就是len/2。

此处给出dpdp做法,以供学习。

  1. 作者:Uncle_drew
  2. 链接:https://ac.nowcoder.com/discuss/363155?type=101
  3. 来源:牛客网
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7. const int maxn=1e6+;
  8. map<int,int>mp;
  9. string code[];
  10. char s[maxn];
  11. int dp[maxn];
  12. void init(){
  13. code[]=".-"; code[]="-..."; code[]="-.-."; code[]="-..";
  14. code[]="..-."; code[]="--."; code[]="....";
  15. code[]=".."; code[]=".---"; code[]="-.-"; code[]=".-..";
  16. code[]="--"; code[]="-."; code[]="---"; code[]=".--.";
  17. code[]="--.-";code[]=".-."; code[]="...";
  18. code[]="..-"; code[]="...-"; code[]=".--";code[]="-..-";
  19. code[]="-.--"; code[]="--..";
  20. for(int i=;i<=;i++){
  21. int num=;
  22. for(int j=;j<code[i].length();j++) num=num*+code[i][j];
  23. mp[num]++;
  24. }
  25. }
  26. int cal(int a,int b){
  27. int num=;
  28. for(int i=a;i<=b;i++) num=num*+s[i];
  29. return mp[num]?:;
  30. }
  31. void solve(){
  32. scanf("%s",s);
  33. int n=strlen(s);
  34. for(int i=;i<n;i++){
  35. dp[i]=;
  36. for(int j=;j<=;j++){
  37. int y=i-j;
  38. dp[i]=max(dp[i],dp[i-j]+cal(i-j+,i));
  39. }
  40. }
  41. printf("%d\n",dp[n-]);
  42. }
  43. int main()
  44. {
  45. init();
  46. int t;
  47. scanf("%d",&t);
  48. while(t--){
  49. solve();
  50. }
  51. return ;
  52. }

-

Morse code(多模式串匹配)的更多相关文章

  1. morse code

    morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代 ...

  2. Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)

    在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...

  3. 模式串匹配之KMP算法

    模式串匹配之KMP算法 KMP算法 模式值计算(next[j]) (1) next[0]=-1,  第一个字符模式值为-1 (2) next[j]=-1, T中下标为j的字符与首字符相同,且j前面的1 ...

  4. 摩尔斯电码(Morse Code)Csharp实现

    摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...

  5. 模式串匹配KMP详解

    关于KMP模式串匹配网上蛮多的. 对于KMP有自己理解所以写下来希望能够对你们的学习有帮助. 之前暑假的时候学过,然后好长时间没用发现又忘了,现在再看看发现有了新的理解. ============== ...

  6. AC自动机——多模式串匹配的算法思想

    标准KMP算法用于单一模式串的匹配,即在母串中寻求一个模式串的匹配,但是现在又存在这样的一个问题,如果同时给出多个模式串,要求找到这一系列模式串在母串存在的匹配个数,我们应该如何处理呢? 基于KMP算 ...

  7. 基于EF Core的Code First模式的DotNetCore快速开发框架

    前言 最近接了几个小单子,因为是小单子,项目规模都比较小,业务相对来说,也比较简单.所以在选择架构的时候,考虑到效率方面的因素,就采取了asp.net+entity framework中的code f ...

  8. 关于EF Code First模式不同建模方式对建表产生的影响

    今天在学EF Code First模式的时候,发现几个很有趣的问题,问题如下: 1.当编写玩实体后,不指定任何主键约束,EF会找长的最像Id的,然后设置其为主键,验证代码如下: //User类 cla ...

  9. Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题

    参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...

随机推荐

  1. maven-本地安装jar包

    maven 安装本地jar包,通过install插件的install-file mojo进行工作,具体可通过如下命令进行查看 mvn help:describe -Dplugin=install -D ...

  2. JAVA - SpringBoot项目跨域访问

    JAVA - SpringBoot添加支持CORS跨域访问 CORS(Cross-Origin Resource Sharing)“跨域资源共享”,是一个W3C标准,它允许浏览器向跨域服务器发送Aja ...

  3. 08 SSM整合案例(企业权限管理系统):10.权限关联与控制

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户操作 10.权限关联与控制 11.AOP日志 10.权限关联与控制 1.用户 ...

  4. SpringMVC: JSON

    SpringMVC:JSON讲解 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛. 采用完全独立于编 ...

  5. windows Driver 查询指定键值

    NTSTATUS status; HANDLE hKey = NULL; OBJECT_ATTRIBUTES oa; UNICODE_STRING strPath = RTL_CONSTANT_STR ...

  6. X2安装配置keras环境(包含matplotlib安装)

    https://blog.csdn.net/jonado13/article/details/83933453 1.安装pipapt install python3-pipE: Could not o ...

  7. UITextFeild的基本属性

    textField 基本属性   _textField.frame = CGRectMake(0, 0, 200, 50); _textField.delegate = self; _textFiel ...

  8. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

  9. HDU——Monkey and Banana 动态规划

                                                                       Monkey and Banana Time Limit:2000 ...

  10. DevOps专题|Lua引擎打造超轻量级客户端

    Lua 作为一门轻量级脚本语言,源码使用标准C语言发布,语法简洁,非常适合嵌入式.客户端.游戏等场景. Lua引擎语言特点 轻量级 源码简单,以lua最新版5.3.5为例,加上lua自身提供的lib库 ...