最长单词连

只能通过某种规定的数据  不能通过所有种类的数据

package hyf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;


public class Longest {

public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
int f=0;
ArrayList<String> a=new ArrayList<>();
a.add("a");

//从文件中获取字符串
String str = readTxtFile("d:\\文件\\input.txt");
if (str == null) {
System.out.println("文件内容获取失败!");
}else
{
//调用函数获取结果
String result = findTheLongestChain(str);

//打印结果
System.out.println(result);

//把结果写入到文件中
exportFile(result);
}
}

//从文件中获取字符串
public static String readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在

InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式

BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
String lineText="";
while((lineTxt = bufferedReader.readLine()) != null)
{
lineText+=(lineTxt);
}
read.close();
return lineText;
}
else
{
System.out.println("找不到文件");
}
} catch (Exception e) {
System.out.println("读取出错");
e.printStackTrace();
}
return null;
}

//寻找最长的单词连
public static String findTheLongestChain(String str) {
//先让最终结果为空
String resultEnd="";

//最长单词链的单词数为空
int counterEnd=0;

//分割字符串获取单词
String[] sList = str.split("[^a-zA-Z]+");

// Java动态数组的初始化
ArrayList al = new ArrayList();
// 向Java动态数组中添加数据
for(int i=0;i<sList.length ;i++) {
al.add(1);
}

int k=2;

//判断有几个单词
if (str == "") {
System.out.println("文件中没有单词");
}else if (sList.length == 1) {
System.out.println("文件中只有一个单词");
}else { //当出现三个单词以上的情况

//创建首字母的数组
char[] headOfWord = new char[sList.length];

//创建尾字母的数组
char[] rearOfWord = new char[sList.length];

//打印一下单词总数
System.out.println("原有"+sList.length+"个单词");

//找所有单词的首尾字母
for(int i = 0 ; i < sList.length ; i++)
{
//当
if (sList[i] != null) {

//记录当前的字母长度 为 找为字母准备
int length = sList[i].length();

//记录下所有的首字母 按顺序
headOfWord[i] = sList[i].charAt(0);

//记录下所有的尾字母 按顺序
rearOfWord[i] = sList[i].charAt(length-1);
}
}

//以每一个单词当做第一个单词 用这个单词的尾部 找到该单词开头的最长链
for(int j = 0 ; j < headOfWord.length; j++)
{
int counter=0;
String result = "";
char wordNow=rearOfWord[j];

//以哪个单词开头 就直接记为0
al.set(j, 0);

//标记为1的时候可用 为0则不可用
for(int g=0;g<al.size();g++) {
if((int)al.get(g)==0) {
continue;
}else {
if (wordNow == headOfWord[g]) {
//如果是第一轮
if(result=="") {
//首字母的单词tag设为0
al.set(g, 0);
//加上结果
result += sList[j] + " "+sList[g]+" ";
counter=counter+2;
}
else {
//如果result有值
al.set(g, 0);
result += sList[g]+" ";
counter++;
}
//当前的单词切换
wordNow=rearOfWord[g];
g=0;
}
}
// System.out.println("counter:"+counter);
if(counterEnd<counter) {
counterEnd=counter;
resultEnd=result;
initTag1(al);
}
}
}
System.out.println("最长链的长度为:"+counterEnd);
}
if (resultEnd == "") {
System.out.println("没有收尾相连");
}
return resultEnd;
}

//初始化Tag[]
public static void initTag1(ArrayList tag) {
int j=0;
for (Object t : tag) {
tag.set(j, 1);
j++;
}
}

//导出最长的单词连到文件
public static void exportFile(String a) throws IOException
{
File file=new File("d:\\文件\\output.txt");
FileOutputStream fos=new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
osw.append(a);
osw.close();
fos.close();
}
}

猜你喜欢

转载自www.cnblogs.com/hyf001/p/11580342.html