世界五百强面试题求解,看似超简单,但是。。。

thunderbird521 2014-09-18 11:29:40
String input = "ABABABA";
写算法求所有符合A$A的字符串的数量,其中$代表A-Z之间至少一个字符。
我一看tnd不就是正则吗?
迅速写出答案:
String regex = "A[A-Z]+A";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group());
}
输出了整个串ABABABA,这与题目要求是不符的!!!
正确答案是6:,即:ABA,ABA,ABA,ABABA,ABABA,ABABABA

求高手赐予我算法!跪谢!
...全文
465 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
bree06 2014-09-24
  • 打赏
  • 举报
回复
既然是算法就应该优先考虑题目要求,所以你不应该考虑A$C或A$B这些情况。A$A就是A$A,不然就不叫算法而是一个普通的题目。
		String input = "ABABABA";
		int countInput = input.length() - input.replace("A", "").length();
		System.out.println(countInput*(countInput-1)/2);
canghailan 2014-09-22
  • 打赏
  • 举报
回复
引用 11 楼 canghailan 的回复:

String input = "ABABABA";
String regex = "A[A-Z]+A";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
for (int i = 0; i < input.length(); ++i) {
	for (int j = i + 1; j <= input.length(); ++j) {
		String substring = input.substring(i, j);
		if (p.matcher(substring).matches()) {
			System.out.println(substring);
		}
	}
}
大体思路就是遍历所有子串,判断是否匹配。 还有优化的空间,比如根据子串的长度。
canghailan 2014-09-22
  • 打赏
  • 举报
回复

String input = "ABABABA";
String regex = "A[A-Z]+A";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
for (int i = 0; i < input.length(); ++i) {
	for (int j = i + 1; j <= input.length(); ++j) {
		String substring = input.substring(i, j);
		if (p.matcher(substring).matches()) {
			System.out.println(substring);
		}
	}
}
boybaozi 2014-09-22
  • 打赏
  • 举报
回复
引用 9 楼 thunderbird521 的回复:
哇塞,牛逼啊,结果都是对的!不过还得麻烦下,能不能输出所有符合条件的字符串(可重复),并且考虑$在末尾的情况
没太明白。。lz的意思啊。。。为什么不通用。。 如果A开头,之后没有A了,不就什么都不输出么。。。 题目不是"$代表A-Z之间至少一个字符"么,为什么还要考虑$。。? 。。。难道说你的意思是想要传参???

	private void getAToA(String input){
		char[] inpchar=input.toCharArray();
		for (int i = 0; i < inpchar.length; i++) {
			if (inpchar[i]=='A') {//first A		
				String atemp="A";//init result
				for (int j = i+1; j < inpchar.length; j++) {
					atemp+=inpchar[j];
					if(inpchar[j]=='A'){
						System.out.println(atemp);
					}
				}
				atemp="";
			}
		}
	}
thunderbird521 2014-09-19
  • 打赏
  • 举报
回复
哇塞,牛逼啊,结果都是对的!不过还得麻烦下,能不能输出所有符合条件的字符串(可重复),并且考虑$在末尾的情况
thunderbird521 2014-09-18
  • 打赏
  • 举报
回复
这只是第一个case,后面还有很多。不一定是A$A,还可以是A$C或者A$B$C,输入的模式串不确定,你要写个通用的算法
白开水MD5 2014-09-18
  • 打赏
  • 举报
回复
找出字符串中A的位置,然后对不连续的A排列组合,连续的在做其他处理
h441887205 2014-09-18
  • 打赏
  • 举报
回复
听说好像有一种KMP算法
a12939026 2014-09-18
  • 打赏
  • 举报
回复
package csdn;

public class Regular {

	String s;
	String r;

	public static void main(String[] args) {
		System.out.println(new Regular("ABABABA","A$A").getResultForRegular(0, 0) == 6);
		System.out.println(new Regular("ABABABA","A$C").getResultForRegular(0, 0) == 0);
		System.out.println(new Regular("ABABABA","A$B$C").getResultForRegular(0, 0) == 0);
		System.out.println(new Regular("ABABABC","A$B$C").getResultForRegular(0, 0) == 1);
		System.out.println(new Regular("ABABABACDAC","A$B$C").getResultForRegular(0, 0) == 6);
		System.out.println(new Regular("ABABABACDAC","A$C").getResultForRegular(0, 0) == 7);
		System.out.println(new Regular("ABABABACDAC","B$C").getResultForRegular(0, 0) == 6);
		System.out.println(new Regular("ABABABACDAC","B$A").getResultForRegular(0, 0) == 6);
	}

	public Regular(String s, String r) {
		this.s = s;
		this.r = r;
	}

	public int getResultForRegular(int sBegin, int rBegin) {
		if (r.charAt(rBegin) == '$') {
			if (rBegin == r.length() - 1) {
				return  s.length() - sBegin;
			}
			int result = 0;
			int pos = sBegin; // 下一个非$的字符位置
			while (pos < s.length() - 1) {
				pos++;
				if (s.charAt(pos) == r.charAt(rBegin + 1)) {
					result += getResultForRegular(pos,rBegin + 1);
				}
			}
			return result;
		}
		else {
			if(sBegin == 0){
				int result = 0 ;
				int pos = 0; // 下一个非$的字符位置
				while (pos < s.length() ) {					
					if (s.charAt(pos) == r.charAt(0)) {
						result += getResultForRegular(pos + 1,1);
					}
					pos++;
				}
				return result;
			}
			
			if (r.charAt(rBegin) == s.charAt(sBegin)) {
				if (rBegin == r.length() - 1)
					return 1;
				if (sBegin == s.length() - 1)
					return 0;
				return getResultForRegular(sBegin + 1,
						rBegin + 1);
			}
			return 0;
		}
	}
}
写好了。 其实还可以用动态规划什么的优化下, 不过没力气了。。 还写了几个测试用例,大致就是这样了。
thunderbird521 2014-09-18
  • 打赏
  • 举报
回复
@boybaozi,感谢回复,但你这个算法不通用啊,如果模式串是A$B$C呢
boybaozi 2014-09-18
  • 打赏
  • 举报
回复
	private void getAToA(){
		String input="ABABABA";
		char[] inpchar=input.toCharArray();
		for (int i = 0; i < inpchar.length; i++) {
			if (inpchar[i]=='A') {//first A		
				String atemp="A";//init result
				for (int j = i+1; j < inpchar.length; j++) {
					atemp+=inpchar[j];
					if(inpchar[j]=='A'){
						System.out.println(atemp);
					}
				}
				atemp="";
			}
		}
	}
就酱
thunderbird521 2014-09-18
  • 打赏
  • 举报
回复
我看好跟这个题目的要求好像不一致,麻烦你晚上写一下给我吧,多谢了:)
a12939026 2014-09-18
  • 打赏
  • 举报
回复
用递归和动态规划的思想。 在input 中不断查找模式串中非$字符的位置,直到遍历掉所有。 和下面这题很像。 你可以看看。 其实这道题比下面的还简单点。 http://www.programcreek.com/2012/12/leetcode-regular-expression-matching-in-java/ 要是你还是不明白可回帖。 我晚上回去可以帮你写一写~

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧