Java字符串模式识别
我有一个大约一千个字符长的字符串,由 L、T 和 A 组成。我很确定其中有一个简单的模式,我想知道是否有任何快速简便的方法可以找到它。该字符串会发生变化,因此这不仅仅是一次性的。
我正在寻找的模式是,例如,如果字符串是
LLLLLLLLAATAALLLLALLLLLLAATAALLLATLLLLLAATAALLAALLLLLAATAALL
子字符串LLLLLAATAALL
在此字符串中重复4次。我想搜索这样的子字符串,但我不知道它们在哪里开始、结束、有多少个以及它们在主字符串中的长度。
如果 Java 中有任何工具可以查找此类内容,我们将不胜感激。
恩特
I have a string that is about a thousand characters long composed of L's, T's, and A's. I'm pretty sure there is a simple pattern in it and I'm wondering if there is any quick and easy way of going about finding it. This string changes so that this is not just a one off.
The pattern I'm looking for is for example if the string was
LLLLLLLLAATAALLLLALLLLLLAATAALLLATLLLLLAATAALLAALLLLLAATAALL
The substring LLLLLAATAALL
repeats 4 times in this string. I want to search for substrings like this but I don't know where they start, end, how many there are, and how long they are in the main string.
If there are any tools in Java for looking for this kind of thing any advice would be much appreciated.
nt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,所以我从 此处 获取代码并对其进行调整以跟踪并打印最长重复子串。只需使用 JUnit 运行它即可。
输出:
编辑:回复您的评论。
目前,我只是跟踪最长的重复 SuffixTreeNode(它是 AbstractSuffixTree 中的一个字段)。您可以修改它,以便它跟踪节点的 SortedQueue,按其 stringDepth 排序。
Ok, so I took the code from here and adapted it to keep track of and print the longest repeated substring. Just run it using JUnit.
Output:
EDIT: response to your comments.
Currently I simply keep track of the longest repeating SuffixTreeNode (it's a field in AbstractSuffixTree). You could modify this so it keeps track of a SortedQueue of nodes, ordered by their stringDepth.
您将从以下代码中了解如何对字符串“hi”进行计数;
公共 int countHi(String str) {
int count = 0,i = str.length() - 1;
字符串目标=“嗨”;
for(int j = 0;j < i;j++)
{
if(str.substring(j,j+2).equals(目标))
计数++;}
返回计数;
}
You will get idea about how to do it from the following code to count the String "hi";
public int countHi(String str) {
int count = 0,i = str.length() - 1;
String goal = "hi";
for(int j = 0;j < i;j++)
{
if(str.substring(j,j+2).equals(goal))
count++;}
return count;
}