- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在做一个来自 this blog 的问题
One day, Jamie noticed that many English words only use the letters A and B. Examples of such words include "AB" (short for abdominal), "BAA" (the noise a sheep makes), "AA" (a type of lava), and "ABBA" (a Swedish pop sensation).
Inspired by this observation, Jamie created a simple game. You are given two Strings: initial and target. The goal of the game is to find a sequence of valid moves that will change initial into target. There are two types of valid moves:
Add the letter A to the end of the string.
Reverse the string and then add the letter B to the end of the string.Return "Possible" (quotes for clarity) if there is a sequence of valid moves that will change initial into target. Otherwise, return "Impossible".
我的问题:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(canContain("B","ABBA"));
}
public static String canContain(String Initial, String Target){
char[] target = new char[1000];
char[] initial1 = new char[1000];
int flag = 0;
boolean possible = false;
int InitialLength = Initial.length();
int TargetLength = Target.length();
System.out.println("Initial:");
int countInitial = -1;
for(char x : Initial.toCharArray()){
countInitial++;
if(x=='A')initial1[countInitial]='A';
if(x=='B')initial1[countInitial]='B';
System.out.print(x+"->"+initial1[countInitial]+" ");
}
int countTarget = -1;
System.out.println("\nTarget:");
for(char y : Target.toCharArray()){
countTarget++;
if(y=='A')target[countTarget]='A';
if(y=='B')target[countTarget]='B';
System.out.print(y+"->"+target[countTarget]+" ");
}
System.out.print("\n");
//Check Initial char[]
System.out.print("---------------");
System.out.print("\n");
for(int t1 = 0; t1 <= countInitial; t1++){
System.out.print(initial1[t1]+"-");
}
System.out.print("\n");
for(int t3 = 0; t3 <= countTarget; t3++){
System.out.print(target[t3]+"-");
}
while(countInitial != countTarget){
if(flag == 0 && Initial != Target){
System.out.println("\n_______A_______");
countInitial++;
System.out.println("countInitial = "+countInitial);
initial1[countInitial] = 'A';
System.out.println(initial1[countInitial]);
for(int t1 = 0; t1 <= countInitial; t1++){
System.out.print(initial1[t1]+"-");
}
flag = 1;
}else if(flag == 1 && Initial != Target){
System.out.println("\n_______R_+_B_______");
int ct = 0;
char[] temp = new char[1000];
for(int i = countInitial; i >= 0; i--){
System.out.println("countInitial = "+countInitial);
temp[ct] = initial1[i];
System.out.println("ct = "+ct);
ct++;
}
initial1 = temp;
countInitial++;
initial1[countInitial] = 'B';
for(int t1 = 0; t1 < countInitial; t1++){
System.out.print(initial1[t1]+"-");
}
flag = 0;
}
}
if(initial1.equals(target)){
return "Possible";
}else{
return "Impossible";
}
}
最佳答案
您的直接问题是您按特定顺序应用规则。但是,不禁止连续多次使用相同的规则。因此,要从初始字符串中获取目标字符串,您需要检查所有可能的规则应用程序序列。这被称为组合爆炸。
像这样的问题通常更容易逆向解决。如果目标字符串是 xyzA
,它只能通过规则 1 从 xyz
获得。如果目标字符串是 xyzB
,它只能通过 zyx
的规则 2 获得。所以在伪代码中,
while length(target) > length(initial)
remove the last letter from target
if removed letter is "B"
reverse target
if target == initial
print "Possible"
else
print "Impossible"
当然,逆转不必是明确的。
关于算法 ABBA(SRM 663, DIV 2, 500),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36574134/
我正在查看的示例位于 TurboPower FlashFiler 中数据库。 例如,它有一个文件 ffclcnst.rc,其中包含 FF_CLIENT_STRINGS RCDATA FFCLCNST.
我是 SAP UI5 (Html/JS) 的新手。我目前正在 SRM UI AddOn 环境中开发一个用于自由文本项目订购的表单。当用户填写完表单后,单击“提交”按钮,表单应消失,并且商品应转移回 S
为了安全,我需要使用 srm(安全删除)而不是 rm 来删除一些文件:http://en.wikipedia.org/wiki/Srm_%28Unix%29 我目前将 srm 设置为对我需要删除的任何
我正在尝试解决 JanuszInTheCasino problem其中一个测试用例 (test_one) 失败。我无法弄清楚问题所在。有什么想法吗? 代码如下: import java.util.Ma
我是博弈论的新手,只了解普通的 nim 游戏,您可以在无条件 的情况下从堆中移除石子,最后移除的玩家获胜。但是后来我在阅读时遇到了一个很好的问题 Game theory tutorial on Top
我正在做一个来自 this blog 的问题 One day, Jamie noticed that many English words only use the letters A and B.
下面这个问题是从 Topcoder 的 SRM 149 中提取的。即使在看了解决方案之后,我也不知道如何做这个问题。我已经尝试这个问题一个半小时了,然后又花了一个小时来解决这个问题,但我就是无法理解它
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
谁能解释一下这个问题的解决方法,你可以在这里查看问题: http://community.topcoder.com/stat?c=problem_statement&pm=13204和这里的解决方案:
我正在努力思考如何将 ASTM E-308 应用于啤酒的 SRM 测量。我工作的问题领域要求我们首先通过转换为 Lab* 从 SRM 到 RGB(或 sRGB)进行适当的转换。似乎我用来创建啤酒配方的
问题陈述是here 谁能帮我理解这个问题。问题陈述说, 选择了一个正整数“X”,每个袋子应该至少有“X”个水果,但在示例中他们选择了 0。这里很困惑。 此外,他们说“你从每个袋子里取出了‘X’个水果”
我正在查看随机的 TopCoder 问题,以便在比赛中尝试和改进,我今天遇到了一个问题,我希望得到一些意见。 问题陈述 Teddy 喜欢玫瑰,Tracy 喜欢百合。他们希望把这些花种在一个大花园里。
我是一名优秀的程序员,十分优秀!