java - 如何从给定的句子中找到给定的缩写形式的数量 - java
问题描述
我正在开发一个文字构建游戏。我陷入了以下场景。
我有一个缩写词;另外,我有一句话,我必须从中确定它可以形成的方式的数量。
例如:
1 缩写:ACM
句子:学院关心经理。输出:4。
2:缩写:雷达
句子:无线电附加游侠。输出:6
基本规则:每个单词至少应该使用一个字符来形成缩写
说明:在第二个例子中,从radio这个词中,rad-ra-r可以用来组成缩写,这样跟下面的词就可以实现整个缩写。并且应该维持秩序。
到目前为止,我试图做这样的事情
public static void checkOccurence(String[] in) {
String word = "";
int k = 0, n;
int total = 0, c = 0;
int extra = 1;
for (int i = 1; i < in.length; i++) {
int v = 0;
n = k;
word = in[i].toUpperCase();
if (k < ab.length()) {
for (int j = 0; j < word.length(); j++) {
if (k < ab.length()) {
if (word.charAt(j) == ab.charAt(k)) {
k++;
}
}
}
}
for (int j = 0; j < word.length(); j++) {
for (int l = 0; l < ab.length(); l++) {
if (word.charAt(j) == ab.charAt(l)) {
if (j != l && l < k - 1) {
v += calculateExtra(word, j, l, k);
} else if (j == l && l < k - 1 && calculateExtra(word, j, l, k) != 1) {
c++;
}
}
}
}
v += c;
System.out.println(v);
if (k == n && v > 0) {
v = 0;
}
total += v;
}
if (k == ab.length() && total != 0)
count += total;
if (k == ab.length() && total == 0)
count++;
}
解决方案
我不认为它是完美的,而且我确信还有很多需要改进的地方,但是这里有一个适用于你的例子的解决方案(可能还有其他边缘情况)我写这个作为如何解决这个问题的一个例子。首先,我们计算所有选项,然后检查哪些节点满足所有条件。请注意小写游戏,这很重要!
public class Main {
public static void main(String[] args) {
// write your code here
Node exampleNode = new Node();
exampleNode.mySentence="academy concern manager".toLowerCase();
exampleNode.chars="ACM".toLowerCase().toCharArray();
exampleNode.nextNode();
System.out.println(getOutput(exampleNode));
exampleNode = new Node();
exampleNode.mySentence="Radio addition ranger".toLowerCase();
exampleNode.chars="RADAR".toLowerCase().toCharArray();
exampleNode.nextNode();
System.out.println(getOutput(exampleNode));
}
public static int getOutput(Node node){
int output=0;
if(node.chars.length==0&& node.mySentence.indexOf(" ")==-1){
Node nextNode=node;
String s="";
while (nextNode!=null){
String sub =nextNode.myChar+nextNode.mySentence;
s=replaceLast(s.toLowerCase(),"",sub)+s;
nextNode = nextNode.father;
}
boolean wordWithoutChar = false;
for (String word:s.split(" ")) {
boolean noCharInThisWord=true;
for (char c:word.toCharArray()) {
if(c<='Z'&&c>='A'){
noCharInThisWord = false;
}
}
if(noCharInThisWord){
wordWithoutChar=noCharInThisWord;
}
}
if(!wordWithoutChar){
System.out.println(s);
output++;
}
}
for (Node n:node.nodes) {
output +=getOutput(n);
}
return output;
}
public static String replaceLast(String find, String replace, String string) {
int lastIndex = string.lastIndexOf(find);
if (lastIndex == -1) {
return string;
}
String beginString = string.substring(0, lastIndex);
String endString = string.substring(lastIndex + find.length());
return beginString + replace + endString;
}
public static class Node{
List<Node> nodes = new ArrayList<>();
Node father =null;
char myChar=0;
char[] chars=null;
String mySentence="";
public void nextNode() {
int index=0;
if(chars.length>0){
while (index<mySentence.length()){
if(mySentence.toCharArray()[index]==chars[0]){
Node son = new Node();
son.chars = Arrays.copyOfRange(chars, 1, chars.length);
son.mySentence=mySentence.substring(index+1);
son.father=this;
son.myChar= (char) (chars[0]-32);
son.nextNode();
nodes.add(son);
}
index++;
}
}
return;
}
}
}
输出:
Academy Concern Manager
Academy conCern Manager
acAdemy Concern Manager
acAdemy conCern Manager
4
RADio Addition Ranger
RADio Addition rangeR
RAdio aDdition rAngeR
RAdio adDition rAngeR
Radio ADdition rAngeR
Radio AdDition rAngeR
6
推荐阅读
- python - 如何格式化 Python 3 程序的输入?
- javascript - 通过 Katalon 自动化软件对 Firebase 中的用户进行身份验证?
- r - 有没有办法让“lm”报告使用的观察量(而不是省略)?
- python - 如何使用sklearn在python中查找缺失值
- user-interface - 给定低分辨率环境,我们如何确保图形对象完全居中且始终可见?
- php - 在 laravel 中间件中获取用户当前状态
- ruby-on-rails - 如何链接到用户个人资料的 ID?
- ruby-on-rails - 在 Rails 中加密和在 Salesforce 中解密
- dart - 如何在 Flutter 中添加从 SharedPreferences 中获取的两个数字
- r - 在 htmlOutput 中将一长列单词分成更多列