由头:
题目:虽然是测试的题目,当时觉得很简单,但是没有做出来。和某acmer交流之后,他说了一个更简单的方法,于是实践一下。
输入一串字符串,例如:abc,cde,edf,fffg ,问是否可以首位连接,形成一个字符串?
思路:
只需要取得每个字符串的首尾保存起来,判断每一个“首”,是否可以在“尾”里找到?如果超过两个找不到,那么说明不可以。
java代码:
package yaxin;
public class yaxin {
public static void main(String args[])
{
String []shuru ={"abc","eddf","cde","eee","aaa"};
String before[] = new String[shuru.length];
String after[] = new String[shuru.length];
for(int i=0;i<shuru.length;i++)
{
int length = shuru[i].length();
before[i] = shuru[i].substring(0,1);
after[i] = shuru[i].substring(length-1,length);
System.out.println(before[i]+" "+after[i]);
}
int count=0;
for(int i=0;i<shuru.length;i++)
{
for(int j=0;j<shuru.length;j++)
{
if(before[i].equals(after[j])) // not ==
{
count++;
//after[j]="-";//delete it
j=shuru.length;//back
}
}
}
if(shuru.length-count<2)
System.out.println("1");
else
System.out.println("0");
}
}