当前位置:网站首页>旋变串判断
旋变串判断
2022-07-04 20:34:00 【N. LAWLIET】
问题
给定两个字符串,str1和str2判断str2是否是str1的旋变串。
旋变串定义
示例:
str1 = abcd str2 = acdb
return false;
思想:
分段来看是否护卫旋变串,dp表加上递归。Dp表存0,1,-1值相等为一,不等为-1.
分成俩种四段来比较,顺位的俩段,和逆位置俩段。
代码
public class Code03_ScrambleString {
public static boolean isScramble0(String str1,String str2) {
if((str1==null&&str2!=null)||(str1!=null&&str2==null)) {
return false;
}
if(str1==null&&str2==null) {
return true;
}
if(str1.length()!=str2.length()) {
return false;
}
char[] s1 = str1.toCharArray();
char[] s2 = str2.toCharArray();
return process(s1,0,s1.length,s2,0,s2.length);
}
private static boolean process(char[] s1, int L1, int R1, char[] s2, int L2, int R2) {
if(L1 == R1) {
return s1[L1] == s2[L2];
}
for(int end = L1;end<R1;end++) {
//正位比较
boolean p1 = process(s1, L1, end, s2, L2, L2+end-L1)&&process(s1, end+1, R1, s2, L2+end-L1+1, R2);
//逆位比较
boolean p2 = process(s1, L1, end, s2, R2-(L2+end-L1)+1, R2)&&process(s1, end+1, R1, s2, L2,R2-(L2+end-L1));
if(p1||p2) {
return true;
}
}
return false;
}
public static boolean isScramble1(String str1,String str2) {
if((str1==null&&str2!=null)||(str1!=null&&str2==null)) {
return false;
}
if(str1==null&&str2==null) {
return true;
}
if(str1.length()!=str2.length()) {
return false;
}
char[] s1 = str1.toCharArray();
char[] s2 = str2.toCharArray();
int N = s1.length;
int[][][] dp = new int[N][N][N+1];
return process1(s1,s2,0,0,N,dp);
}
public static boolean process1(char[] s1,char[] s2,int L1,int L2,int size,int[][][] dp) {
if(dp[L1][L2][size]!=0) {
return dp[L1][L2][size] == 1;
}
boolean ans = false;
if(size == 1) {
ans = s1[L1] == s2[L2];
}
for(int end = L1;end<size;end++) {
if((process1(s1,s2, L1+end,L2+end,size-end,dp)&&process1(s1,s2,L1,L2,end,dp))||
(process1(s1, s2, L1, L2+size-end, end, dp)&&process1(s1, s2, L1+end, L2, size-end, dp))) {
ans = true;
break;
}
}
dp[L1][L2][size] = ans?1:-1;
return ans;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String str1 = scanner.nextLine();
String str2 = scanner.nextLine();
boolean ans = isScramble1(str1, str2);
System.out.println(ans);
}
}
}
边栏推荐
- nmap扫描
- Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
- shp数据制作3DTiles白膜
- Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
- torch.tensor和torch.Tensor的区别
- 吐槽 B 站收费,是怪它没钱么?
- Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
- Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
- What are the functional modules of RFID warehouse management system solution
- [buuctf.reverse] 151_ [FlareOn6]DnsChess
猜你喜欢
华为ensp模拟器 配置ACL访问控制列表
c语言函数形参自增自减情况分析
torch. Tensor and torch The difference between tensor
Difference between ApplicationContext and beanfactory (MS)
render函数与虚拟dom
Introduction to pressure measurement of JMeter
Jerry's ad series MIDI function description [chapter]
WGCNA分析基本教程总结
Redis:Redis配置文件相关配置、Redis的持久化
创客思维在高等教育中的启迪作用
随机推荐
redis RDB AOF
redis RDB AOF
minidom 模块写入和解析 XML
Huawei ENSP simulator enables devices of multiple routers to access each other
PS vertical English and digital text how to change direction (vertical display)
杰理之AD 系列 MIDI 功能说明【篇】
华为ensp模拟器 实现多个路由器的设备可以相互访问
Actual combat simulation │ JWT login authentication
Render function and virtual DOM
Gobang go to work fishing tools can be LAN / man-machine
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
多模输入事件分发机制详解
Huawei ENSP simulator realizes communication security (switch)
async await 在map中使用
Maya lamp modeling
LambdaQueryWrapper用法
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
Jerry's ad series MIDI function description [chapter]
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2