Substrings

题目描述:

You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.

输入:

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.

输出:

There should be one line per test case containing the length of the largest string found.

样例输入:

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

样例输出:

2
2

code:

字符串的水题,暴力找到可能的子串,然后KMP判断

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
char a[200][500];
char b[500];
char c[500];
int nextt[500];
int nexxt[500];
void init()
{
	int i=0,j=-1;
	nextt[0]=-1;
	int m=strlen(b);
	while(i<m)
	{
		if(j==-1||b[i]==b[j])
		{
			i++;
			j++;
			nextt[i]=j;
		}
		else
		{
			j=nextt[j];
		}
	}
}
void init2()
{
	int i=0,j=-1;
	nexxt[0]=-1;
	int m=strlen(c);
	while(i<m)
	{
		if(j==-1||c[i]==c[j])
		{
			i++;
			j++;
			nexxt[i]=j;
		}
		else
		{
			j=nexxt[j];
		}
	}
}
bool kmp(int what)
{
	int i=0,j=0;
	int flag=0;
	init();
	int n=strlen(a[what]);
	int m=strlen(b);
	while(i<n)
	{
		if(j==-1||a[what][i]==b[j])
		{
			j++;
			i++;
		}
		else j=nextt[j];
		if(j==m)
		{
			return true;
		}
	}
	return false;
}
bool kmp2(int what)
{
	int i=0,j=0;
	int flag=0;
	init2();
	int n=strlen(a[what]);
	int m=strlen(c);
	while(i<n)
	{
		if(j==-1||a[what][i]==c[j])
		{
			j++;
			i++;
		}
		else j=nexxt[j];
		if(j==m)
		{
			return true;
		}
	}
	return false;
}
bool cmp(string a,string b)
{
	return a<b;
}
int main()
{
	int ttt,n;
	scanf("%d",&ttt);
	while(ttt--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",a[i]);
		}
		int m=strlen(a[0]);
		int flag=0;
		int much;
		int sum=0;
		for(int i=0;i<=m;i++)
		{//减掉几个
			for(int j=0;j<=i;j++)
			{//该字符串前面减掉几个
				int start=j,over=m-i+j-1;//后面减掉几个
				memset(b,'\0',sizeof(b));
				int qq=0;
				for(int k=start;k<=over;k++)
				{
					b[qq++]=a[0][k];
				}
				memset(c,'\0',sizeof(c));
				qq=0;
				for(int k=over;k>=start;k--)
				{
					c[qq++]=a[0][k];
				}
				int ji=1;
				for(int k=1;k<n;k++)
				{
					if(kmp(k)==0&&kmp2(k)==0)
					{
						ji=0;
					}
				}
				if(ji==1)
				{
					flag=1;
				}
				if(flag)break;
			}
			if(flag)break;
		}
		printf("%d\n",strlen(b));
	}
	return 0;
}

<think>嗯,用户给了一段Java代码,看起来有问题。他们想了解这段代码或者得到解释。首先,我需要仔细看一下代码的结构。 代码是: for(String substring:substrings){ System.out.println(substrings); } 用户可能在这里犯了常见的错误。首先,这里使用的是增强型for循环,变量名是substring,但循环体内打印的是substrings,也就是集合本身,而不是每次迭代的substring。这会导致循环每次输出整个集合,而不是每个元素。这应该是个笔误,正确应该是System.out.println(substring);。 接下来,我需要确认substrings这个集合是否存在,是否已经正确初始化。如果substrings没有被定义或者为null,这段代码会抛出空指针异常。这也是需要注意的地方。 然后,我应该考虑可能的场景,用户可能在遍历一个字符串数组或列表,想逐个打印每个子字符串,但因为变量名打错,导致结果不符合预期。这时候需要指出变量名的正确使用,并给出修正后的代码示例。 另外,还要解释增强型for循环的工作原理,确保用户理解循环变量和集合的关系。可能还需要提到常见的错误类型,比如并发修改异常,如果在遍历过程中修改集合的话,但这里的问题更基础,是变量名错误。 总结用户的代码问题,主要是打印了集合对象而不是元素,需要修正变量名,并检查集合是否正确初始化。然后给出正确的代码示例,说明每个部分的作用,帮助用户理解哪里出错了,以及如何修复。</think>你提供的Java代码片段中存在一个常见的逻辑错误,具体分析如下: ### 原代码分析 ```java for(String substring:substrings){ System.out.println(substrings); } ``` #### 问题说明: 1. **变量误用**:循环变量声明为 `substring`(单数),但输出语句中错误地使用了集合变量 `substrings`(复数) 2. **预期行为偏离**:此代码会重复打印整个集合对象(如数组或列表)的地址/哈希值,而非逐个输出元素内容 ### 修正后的代码 ```java for(String substring:substrings){ System.out.println(substring); // 注意变量名改为单数 } ``` ### 关键知识点 1. **增强for循环结构**: ```java for(元素类型 变量名 : 集合/数组){ // 使用当前元素(变量名) } ``` 2. **正确执行条件**: - `substrings` 必须是已初始化的可迭代对象(如数组、List集合) - 如果 `substrings` 为null会抛出 `NullPointerException` ### 完整示例 ```java import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> substrings = Arrays.asList("A", "B", "C"); for(String substring : substrings){ System.out.println(substring); // 正确输出每个元素 } } } ``` ### 输出结果 ``` A B C ``` 建议检查代码中的变量命名一致性,这种错误通常是由于拼写疏忽造成的。在实际开发中,使用IDE的语法高亮功能可以快速识别此类问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值