当前位置:网站首页>Rotary transformer string judgment
Rotary transformer string judgment
2022-07-04 21:34:00 【N. LAWLIET】
problem
Given two strings ,str1 and str2 Judge str2 Whether it is str1 Rotary transformer string of .
Definition of rotary transformer string 
Example :
str1 = abcd str2 = acdb
return false;
thought :
Check whether to protect the spiral transformer string by sections ,dp Table plus recursion .Dp Table storage 0,1,-1 Values equal to one , Don't wait for -1.
Divide into two kinds and four paragraphs to compare , Two paragraphs in sequence , And the reverse position .
Code
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++) {
// Positive comparison
boolean p1 = process(s1, L1, end, s2, L2, L2+end-L1)&&process(s1, end+1, R1, s2, L2+end-L1+1, R2);
// Inverse comparison
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);
}
}
}
边栏推荐
猜你喜欢

Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"

华为ensp模拟器 实现多个路由器的设备可以相互访问

torch. Tensor and torch The difference between tensor

Foxit pdf editor v10.1.8 green version

仿ps样式js网页涂鸦板插件

解读创客教育中的各类智能化组织发展

Daily question -leetcode1200- minimum absolute difference - array - sort

改善机器视觉系统的方法

Detailed explanation of multi-mode input event distribution mechanism

Gobang go to work fishing tools can be LAN / man-machine
随机推荐
TweenMax表情按钮js特效
CAD中能显示打印不显示
杰理之AD 系列 MIDI 功能说明【篇】
maya灯建模
Day24: file system
Jerry's ad series MIDI function description [chapter]
输入的查询SQL语句,是如何执行的?
UTF encoding and character set in golang
Huawei simulator ENSP common commands
[wechat applet] collaborative work and release
heatmap.js图片热点热力图插件
【公开课预告】:视频质量评价基础与实践
MP3是如何诞生的?
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
Redis bloom filter
2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
Procurement in software development
华为ensp模拟器 配置ACL访问控制列表
仿ps样式js网页涂鸦板插件
Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?