题目

给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串,如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解。

请返回所有可行解 s 中最长长度。

解题

暴力回溯:

f(i,bool[],arr):遍历到arr中第i个字符串,bool数组标识已经出现过的字符。

f(i+1,bool[],arr)+arr[i].length()//如果bool和arr[i]没有冲突,则把arr[i]加入bool[],并且递归

回退bool,

f(i+1.bool[],maxLen,arr)

向上返回两者中大的。

优化用32位的int代表26位的bool[],加上memo。注意单个字符串自己可能就不符合条件,可以用-1表示。

代码:

public static int maxLength(List<String> arr) {
//得到每个的长度和数字表示
int n=arr.size();
int[] len=new int[n];
int[] present=new int[n];
for (int i = 0; i < n; i++) {
len[i]=arr.get(i).length();
present[i]=toPresent(arr.get(i));
}
return maxLength(0,0,len,present);
}
//字符串->int表示哪些字符出现过
private static int toPresent(String s) {
int res=0;
for (char ch:s.toCharArray()){
int tmp=1<<((ch-'a'));
if((tmp&res)!=0) return -1;
res|=tmp;
}
return res;
} public static int maxLength(int i,int chars, int[] len,int[] present) {
if(i==len.length){
return 0;
}
int res1=maxLength(i+1,chars,len,present);
boolean conflict=(present[i]==-1||(chars&present[i])!=0); int res2=0;
if(!conflict){
res2=len[i]+maxLength(i+1,chars|present[i],len,present);
} return Math.max(res1,res2);
}

LeetCode1239串联字符串的最大长度的更多相关文章

  1. Leetcode 1239. 串联字符串的最大长度

    地址 https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/s ...

  2. linux shell 字符串操作(长度,查找,替换)详解

    linux shell 字符串操作(长度,查找,替换)详解 在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作. 其实shell内置一系 ...

  3. 46. 对称子字符串的最大长度(ToDo)

    [题目] 输入一个字符串,输出该字符串中对称的子字符串的最大长度.比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. [分析] 可能很多人都写过判断一个字符串 ...

  4. squee_spoon and his Cube VI(贪心,找不含一组字符串的最大长度+kmp)

    1818: squee_spoon and his Cube VI Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 77  Solved: 22Subm ...

  5. shell脚本—— 字符串操作(长度,查找,替换)

    表达式 含义 ${var} 变量var的值, 与$var相同 ${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 * ${var:-DEFAULT} 如果var没有 ...

  6. linux的string操作(字符串截取,长度计算)

    按指定的字符串截取 1.第一种方法: ${varible##*string} 从左向右截取最后一个string后的字符串 ${varible#*string}从左向右截取第一个string后的字符串 ...

  7. 已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数

    import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 21:04 * @description ...

  8. C#返回字符串的字节长度,一个中文算两个字符的代码

    如下代码段是关于C#返回字符串的字节长度,一个中文算两个字符的代码. public static int GetLength(string str) { if (str.Length == 0) re ...

  9. 字符串分隔 ->连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

        •连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组:•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理. 输入描述: 连续输入字符串(输入2次,每个字符串长度小于100 ...

随机推荐

  1. liveBOS环境搭建

    环境搭建:1.准备jdk1.6及以上版本oracle11gplsqlsql脚本(oracle_init.sql,oracle_insert.sql)livebos_tomcatlivebos的授权文件 ...

  2. 【Maven实战技巧】「插件使用专题」Maven-Archetype插件创建自定义maven项目骨架

    技术推荐 自定义Archetype Maven骨架/以当前项目为模板创建maven骨架,可以参考http://maven.apache.org/archetype/maven-archetype-pl ...

  3. Mybatis相关知识点(一)

    MyBatis入门 (一)介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code, ...

  4. navicate连接Mysql5.7时,显示Access denied for user 'root'@'localhost' (using password: YES) 错误

    最近新装了Mysql5.7,按如下设置好了允许远程连接    (1)找到mysql配置文件并修改 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 将bind-ad ...

  5. 输入URL展示过程

    一. 输入URL,回车 敲击某个键时,键盘内的处理器会先对键矩阵进行分析,然后将数据发送到计算机 计算机接收到来自键盘的信号,由键盘控制器(一种集成电路)进行处理,发送给操作系统 操作系统会分析,这些 ...

  6. Mysql百万级数据索引重新排序

    参考https://blog.csdn.net/pengshuai007/article/details/86021689中思路解决自增id重排 方式一 alter table `table_name ...

  7. mysql 报 'Host ‘XXXXXX’ is blocked because of many connection errors'

    1. 问题:服务启动时,日志报错,导致启动失败: Caused by: com.mysql.cj.exceptions.CJException: null,  message from server: ...

  8. javascript将平行的拥有上下级关系的数据转换成树形结构

    转换函数 var Littlehow = {}; /** * littlehow 2019-05-15 * 平行数据树形转换器 * @type {{format: tree.format, sort: ...

  9. 如何将java对象转换成json数据

    package cn.hopetesting.com.test;import cn.hopetesting.com.domain.User;import com.fasterxml.jackson.c ...

  10. Mysql配置文件 innodb引擎

    目录 innodb参数 innodb_buffer_pool_size innodb_read_io_threads|innodb_write_io_threads innodb_open_files ...