Java 生成最短摘要 修改版

算法很美中讲的方法存在一定问题

对于已经查找到的范围缩减范围后存在更小区间的情况没有被考虑进去 个人认为原因出在辅助数组直接全部清0这个地方 中间已经找到的元素被全部置0导致后面搜索时需要继续向后端遍历

仅仅代表个人愚见 欢迎各位大神指正!

代码如下:

package 最短摘要;

import java.util.Arrays;

/*
 * 
 * 给定一段产品的英文描述,包含M个英文单词,每个英文单词以空格分隔,无其他标点符号;
在给定N个英文关键字,请说明思路并编程实现方法
 * 
 * */
public class Main {

	public static void main(String[] args) {
		String[] s = new String[]{"d","b","b","a","c","s","d","a","t"};
        String[] keyWords = new String[] {"a","b","c"};
        extract(s, keyWords);
        
	}
	
	
	public static void extract(String[] s,String[] keyWords) {
		int sLength = s.length;//原字符串数组长度
		int kLength = keyWords.length;//搜索字符串数组长度
		int begin = -1;//用于记录开始位置字符串
		int end = -1;//用于记录结束位置字符串
		int p2 = -1;//p2用于保存每一次所保留的范围中j的位置
		int[] keyFound = new int[kLength];//建立一个辅助数组,长度等于搜索字符串长度,用于记录该字符串是否已经被搜索到。
		int minlength = sLength + 1;
		for (int i = 0; i < sLength; i++) {
			//Arrays.fill(keyFound, 0);
			String word = s[i];//当前i位置的字符串
			int index = containsOf(keyWords, word);
			if(index == -1) {//判断该字符串是否在搜索字符串数组中
				continue;
			}else {
				keyFound[index] = 1;//将辅助数组中对应的搜索字符位置处置1作为标记
			}
			
			int j;
			if(p2 == -1) {
				j = i + 1;
			}else {
				j = p2;
			}
			for (; j < sLength; j++) {
				String word1 = s[j];//当前i位置的字符串
				int index1 = containsOf(keyWords, word1);
				if(index1 == -1 || keyFound[index1] == 1) {//判断该字符串是否在搜索字符串数组中
					continue;
				}else {
					keyFound[index1] = 1;//将辅助数组中对应的搜索字符位置处置1作为标记
					
				}
				if(sum(keyFound) == keyFound.length) {//找齐了所有元素
					System.out.println("探测尝试:"+i+"   "+j);
					p2 = j;//保存探测到的右边界
					if(j - i + 1 < minlength) {
						begin = i;
						end = j;
						minlength = j - i + 1;
						keyFound[index] = 0;//这里要考虑对于左边界缩小 右边界不动的情况 如果中间存在其他已经搜索到的元素 那么他们不需要被置0
						keyFound[index1] = 0;
					}
					break;
				
				}
			}
			
			
			
		}
		System.out.println("最终结果起点下标为:"+begin +" 下标终点为:"+ end);
		
				
	}
	//检索搜索字符串数组中是否包含该str字符串,包含则返回该字符串在搜索字符串的下标
	public static int containsOf(String[] keyWords ,String str ) {
		for (int i = 0; i < keyWords.length; i++) {
			if(keyWords[i].equals(str))
				return i;
		}
		return -1;
	}
	
	//检索辅助数组中所有元素的和用来判断是否每一个搜索字符串都被找到
	public static int sum(int[] arr) {
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}
		return sum;
	}
	
	
	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现最短路径算法通常使用Dijkstra算法或者Bellman-Ford算法。 以下是使用Dijkstra算法实现最短路径的Java代码示例: ```java import java.util.*; public class DijkstraAlgorithm { private static final int NO_PARENT = -1; private static void dijkstra(int[][] adjacencyMatrix, int startVertex) { int nVertices = adjacencyMatrix[0].length; int[] shortestDistances = new int[nVertices]; boolean[] visited = new boolean[nVertices]; shortestDistances[startVertex] = 0; int[] parents = new int[nVertices]; parents[startVertex] = NO_PARENT; for (int i = 1; i < nVertices; i++) { int nearestVertex = -1; int shortestDistance = Integer.MAX_VALUE; for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { if (!visited[vertexIndex] && shortestDistances[vertexIndex] < shortestDistance) { nearestVertex = vertexIndex; shortestDistance = shortestDistances[vertexIndex]; } } visited[nearestVertex] = true; for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { int edgeDistance = adjacencyMatrix[nearestVertex][vertexIndex]; if (edgeDistance > 0 && ((shortestDistance + edgeDistance) < shortestDistances[vertexIndex])) { parents[vertexIndex] = nearestVertex; shortestDistances[vertexIndex] = shortestDistance + edgeDistance; } } } printSolution(startVertex, shortestDistances, parents); } private static void printSolution(int startVertex, int[] distances, int[] parents) { int nVertices = distances.length; System.out.print("Vertex\t Distance\tPath"); for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { if (vertexIndex != startVertex) { System.out.print("\n" + startVertex + " -> "); System.out.print(vertexIndex + " \t\t "); System.out.print(distances[vertexIndex] + "\t\t"); printPath(vertexIndex, parents); } } } private static void printPath(int currentVertex, int[] parents) { if (currentVertex == NO_PARENT) { return; } printPath(parents[currentVertex], parents); System.out.print(currentVertex + " "); } public static void main(String[] args) { int[][] adjacencyMatrix = { {0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0} }; dijkstra(adjacencyMatrix, 0); } } ``` 在上面的示例中,我们使用了邻接矩阵来表示图,并使用了Dijkstra算法来计算最短路径。使用邻接表也可以实现相同的算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值