我需要创建具有字母表的DFA:{a,b,c} &这个字母表接受第一个字母和最后一个字母不同的单词。即
"a“是不可接受的
"ab“-可接受
"aaa bb“--是不可接受的。
"cbba“-可接受
我首先试着检查开头是否有"a“,但有些地方出了问题,特别是如果我有i.o。file.txt中的"ab“或"ac”
来源:
import java.io.*;
import java.util.ArrayList;
public class Task
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
BufferedReader reader = new BufferedReader(new FileReader("file.txt "));
ArrayList<String> wordList = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null)
{
wordList.add(line);
}
for (String word : wordList)
{
if (word.matches("^a"))
{
if (word.matches("ab") || word.matches("^ac"))
{
System.out.print(word+" - OK\n");
}
else
{
System.out.print(word+" - STOP (word doesn't exists in alphabet)\n");
System.exit(0);
}
}
}
}
}发布于 2016-11-01 18:46:46
您的第一个"words.matches“将只匹配"a",如果您想匹配以"a”开头的所有单词,则后面跟着必须使用"^a.*“的其他单词,其他匹配也一样。
https://stackoverflow.com/questions/40366495
复制相似问题