当前位置:网站首页>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);
}
}
}
边栏推荐
- redis管道
- 2021 CCPC Harbin B. magical subsequence (thinking question)
- Huawei ENSP simulator enables devices of multiple routers to access each other
- __ init__ () missing 2 required positive arguments
- 华为ensp模拟器 三层交换机
- Jerry's ad series MIDI function description [chapter]
- 【C语言】符号的深度理解
- B站视频 声音很小——解决办法
- 杰理之AD 系列 MIDI 功能说明【篇】
- In the release version, the random white screen does not display the content after opening the shutter
猜你喜欢

Maidong Internet won the bid of Beijing life insurance

杰理之AD 系列 MIDI 功能说明【篇】

js 3D爆炸碎片图片切换js特效

Introduction to pressure measurement of JMeter

Configuration of DNS server of Huawei ENSP simulator

Methods of improving machine vision system

Flutter TextField示例

OMS系统实战的三两事

【C語言】符號的深度理解

Arcgis 10.2.2 | arcgis license server无法启动的解决办法
随机推荐
colResizable.js自动调整表格宽度插件
Maya lamp modeling
At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
Jerry's ad series MIDI function description [chapter]
Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
redis RDB AOF
吐槽 B 站收费,是怪它没钱么?
Huawei ENSP simulator layer 3 switch
Introduction to pressure measurement of JMeter
redis RDB AOF
Jerry's ad series MIDI function description [chapter]
js 3D爆炸碎片图片切换js特效
shp数据制作3DTiles白膜
LambdaQueryWrapper用法
heatmap.js图片热点热力图插件
插入排序,选择排序,冒泡排序
The video sound of station B is very low - solution
maya灯建模
华为ensp模拟器 三层交换机
Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system