Raine_Yang 2021-02-16 20:56 采纳率: 50%
浏览 151

ACSL竞赛题Sameness Factor求解

原题如下:

PROBLEM: Given 2 strings, separated by a space, calculate the ACSL Sameness Factor (ASF). Process the strings using following steps: string. Therefore, the A would be deleted and the NTI shifted to the left. Repeat this step until nore more characters to delete. to the ASF. Calculate the ACSL Sameness Factor by doing the following:

● Align the strings from left to right.

● Delete the like characters in the like locations from left to right.

● Proceeding from left to right, if the like location characters are not the same and deleting a character at a location in one of the strings which shifts the remaining characters to the left causes like characters to be at that location, delete those characters and any other like characters at like locations. If there is a case as in NAPE and ANTI where it is possible to delete a character at the same location in both strings, then delete it in the second

● Calculate the difference in the alphabetic locations from the aligned string characters in

● If there are characters remaining in one of the strings, add the number of those characters the second string to the string character in the first string. B to D would add 2 to the ASF. D to B would add -2 to the ASF.

 

Example: ABCDEFT ABXCGBTZFP ABCDEFT → ABCDEFT → CDEF → CDEF → DEF → DEF → DE ABXCGBTZFP → ABXCGBTZFP →XCGBZFP → CGBZFP → GBZFP → GBFP → GBP The ASF is calculated as: G to D = -3 B to E = + 3 P = + 1 (-3 + 3 + 1 = 1)

 

INPUT: There will be 5 inputs. Each input will contain 2 strings separated by a space and each fewer than 200 characters.

 

OUTPUT: For each input, print the ASF as described above. SAMPLE INPUT SAMPLE OUTPUT: (http://www.datafiles.acsl.org/2020/contest2/int-sample-input.txt)

BLAMEABLENESSES BLAMELESSNESSES                    1. -35

MEZZAMINES RAZZMATAZZ                                              2. -5

ABBREVIATIONS ABBREVIATORS                                     3. -4、

ABCDEFGHIJKLMNO ABKCLDZZHQJWWLX                     4. -86

ABCDEFGHIJKL ABXEWFRRH                                           5. -52

以下是我写的程序,自己一点问题查不出来,但是运行结果就是和给的示例不一样,到底是程序有什么bug,还是我把题理解错了?

import java.util.*;

public class Practice2 {

	public static String[] deleteSame (String[] origin) {
		//like character in like location
		String list1 = origin[0];
		String list2 = origin[1];
		String[] result = new String[2];
		boolean remain = false;
		
		
		//find the shortest string, in case of out of boundary error
		int loop = 0;
		if (list1.length() > list2.length()) {
			loop = list2.length();
		} else {
			loop = list1.length();
		}
		
		
		//identify and delete the like characters
		for (int i = 0; i < loop; i++) {
			if (list2.contains(list1.substring(i, i + 1))) {
			 if (list2.substring(i, i + 1).equals(list1.substring(i, i + 1))) {
			    list2 = list2.substring(0, i) + list2.substring(i + 1);
			    list1 = list1.substring(0, i) + list1.substring(i + 1);
			    remain = true;
			    loop--;
			 }
			}
		}
		result[0] = list1;
		result[1] = list2;
		if (!remain) {
			return result;  //continue this process until all the characters are deleted
		} else {
			return deleteSame(result);
		}
	}
	
	
	public static String[] deleteDifferent (String[] origin) {
		//like character in different locations
		String list1 = origin[0];
		String list2 = origin[1];
		String[] result = new String[2];
		boolean remain = false;
		
		
		int loop = 0;
		if (list1.length() > list2.length()) {
			loop = list2.length();
		} else {
			loop = list1.length();
		}
		
		
		for (int i = 0; i < loop; i++) {
		  if (list2.contains(list1.substring(i, i + 1))) {
		   if (list2.substring(i + 1, i + 2).equals(list1.substring(i, i + 1))) {  //delete the character before list2 and the like character
			   list2 = list2.substring(0, i) + list2.substring(i + 2);
			   list1 = list1.substring(0, i) + list1.substring(i + 1);
			   remain = true;
			   loop--;
		   } else if (list1.substring(i + 1, i + 2).equals(list2.substring(i, i + 1))) { //delete the character before list1 and like character
		       list1 = list1.substring(0, i) + list1.substring(i + 2);
		       list2 = list2.substring(0, i) + list2.substring(i + 1);
		       remain = true;
		       loop--;
		   }
		   } 
		}
		
		
		
		
		result[0] = list1;
		result[1] = list2;
		if (!remain) {
			return result;
		} else {
			return deleteDifferent(result);
		}
	}
	
	
	public static int getValue (String[] lists) {
		char[] words1 = lists[0].toCharArray();
		char[] words2 = lists[1].toCharArray();
		int result = 0;
		
		
		//find the longer array
		if (words2.length < words1.length) {
			for (int i = 0; i < words2.length; i++) {
				result += words1[i] - words2[i];  //calculate the sameness factor between each letter
			}
			for (int i = words2.length; i < words1.length; i++) {
				result++;   //add the remaining letters
 			}
		} else {
			for (int i = 0; i < words1.length; i++) {
				result += words1[i] - words2[i];
			}
			for (int i = words1.length; i < words2.length; i++) {
				result++;
			}
		}
		
		return result;
		
	}	
	
	
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
    Scanner input = new Scanner (System.in);
    String origin = input.nextLine();
    String[] temp = origin.split(" ");
    System.out.println(getValue(deleteDifferent(deleteSame(temp))));
    
	}

}
  • 写回答

1条回答 默认 最新

  • Raine_Yang 2021-02-19 21:17
    关注

    顶一下……

    评论

报告相同问题?

悬赏问题

  • ¥40 matlab调用ansys联合
  • ¥15 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下!
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)