user-0 提问: 蛮有意思的一题

回复:
user: user-1
content: 所需要时间， 一毫秒不到。时间为O(n), 空间为常数    int cr(const char* c, const int a,  char* r) { 	r[0] = c[0]; 	int n = 1; 	for (int i = 1; i < a; i++) 	{ 		bool b = 0; 		for (int j = 0; j < n; j++) 		{ 			if (c[i] == r[j]) 			{ 				b = 1; 				break; 			} 		} 		if (!b) 			r[n++] = c[i]; 	}  	r[n] = 0; 	return n; } int ma(const char* m, const int a, const char* r, const int b, const char** tb ) { 	const char* srb = 0; 	int srn = a+1;  	const char* rt[0xff] = {0}; 	int rtn = 0; 	for (int i = 0; i < a; i++) 	{ 		int fat = -1; 		for (int j = 0; j < b; j++) 		{ 			if (m[i] == r[j]) 			{ 				fat = j; 				break; 			} 		} 		if (fat <0) 			continue; 		bool blnk = 0 == rt[fat] ; 		rt[fat] = m +i; 		if (blnk) 		{ 			rtn++; 			if (rtn >= b) 			{ 				const char* curb = (const char*)(-1), *cure = 0; 				int curbN = 0; 				for (int k = 0; k < b; k++) 				{ 					if (curb > rt[k]) 					{ 						curbN = k; 						curb = rt[k]; 					} 					if (cure < rt[k]) 						cure = rt[k]; 				} 				int curN = cure - curb; 				if (curN < srn) 				{ 					srb = curb; 					srn = curN; 				}  				rt[curbN] = 0; 			} 		}  	}  	if (!srb) 		return 0; 	*tb = srb; 	return srn+1; }    int ft(const char* y,   const char* x, const char** mr) {  	int yn = strlen(y); 	int xn = strlen(x); 	char r[0xff+1];  	//	去重 	int nc = cr(x, xn, r); 	//	匹配 	//	最短组合 	return  ma(y, yn, r, nc, mr); }  void fft(const char* y, const char* x) { 	const char* mr = 0;  	LARGE_INTEGER lgb, lge, lgt; 	QueryPerformanceFrequency(&lgt);  	QueryPerformanceCounter(&lgb); 	int mrn = ft(y, x, &mr); 	QueryPerformanceCounter(&lge);  	for (const char* b = mr; b < mr + mrn; b++) 	{ 		printf("%c", *b); 	} 	printf(",\tCPU时间戳(毫秒): %f, \r\n", (lge.LowPart - lgb.LowPart)*1000.0f / lgt.LowPart); }  int _tmain(int argc, _TCHAR* argv[]) { 	const char* y = "obzcopzocynyrsgsarijyxnkpnukkrvzuwdjldxndmnvevpgmxrmvfwkutwekrffnloyqnntbdohyfqndhzyoykiripdzwiojyoznbtogjyfpouuxvumtewmmnqnkadvzrvouqfbbdiqremqzgevkbhyoznacqwbhtrcjwfkzpdstpjswnpiqxjhywjanhdwavajrhwtwzlrqwmombxcaijzevbtcfsdcuovckoalcseaesmhrrizcjgxkbartdtotpsefsrjmvksqyahpijsrppdqpvmuocofuunonybjivbjviyftsyiicbzxnwnrmvlgkzticetyfcvqcbjvbufdxgcmesdqnowzpshuwcseenwjqhgsdlxatamysrohfnixfprdsljyyfhrnnjsagtuihuczilgvtfcjwgdhpbixlzmakebszxbhrdibpoxiwztshwczamwnninzmqrmpsviydkptjzpktksrortapgpxwojofxeasoyvyprjoguhqobehugwdvtzlenrcttuitsiijswpogicjolfxhiscjggzzissfcnxnvgftxvbfzkukqrtalvktdjsodmtgzqtuyaqvvrbuexgwqzwduixzrpnvegddyyywaquxjxrnuzlmyipuqotkghfkpknqinoidifnfyczzonxydtqroazxhjnrxfbmtlqcsfhshjrxwqvblovaouxwempdrrplefnxmwrwfjtebrfnfanvvmtbzjesctdgbsfnpxlwihalyiafincfcwgdfkvhebphtxukwgjgplrntsuchyjjuqozakiglangxkttsczhnswjksnuqwflmumpexxrznzwxurrysaokwxxqkrggytvsgkyfjrewrcvntomnoazmzycjrjrqemimyhriyxgrzcfuqtjhvjtuhwfzhwpljzajitrhryaqchnuawbxhxrpvyqcvhpggrpplhychyulijhkglinibedauhvdydkqszdbzfkzbvhldstocgydnbfjkcnkfxcyyfbzmmyojgzmasccaahpdnzproaxnexnkamwmkmwslksfpwirexxtymkmojztgmfhydvlqtddewjvsrmyqjrpycbmndhupmdqqabiuelacuvxnhxgtpvrtwfgzpcrbhhtikbcqpctlxszgpfbgcsbaaiapmtsucocmpecgixshrrnhyrpalralbccnxvjzjllarqhznzghswqsnfuyywmzbopyjyauknxddgdthlabjqtwxpxwljvoxkpjjpfvccyikbbrpdsyvlxscuoofkecwtnfkvcnzbxkeabtdusyhrqklhaqreupakxkfzxgawqfwsaboszvlshwzhosojjotgyagygguzntrouhiweuomqptfjjqsxlbylhwtpssdlltgubczxslqjgxuqnmpynnlwjgmebrpokxjnbiltvbebyytnnjlcwyzignmhedwqbfdepqakrelrdfesqrumptwwgifmmbepiktxavhuavlfaqxqhreznbvvlakzeoomykkzftthoemqwliednfsqcnbexbimrvkdhllcesrlhhjsspvfupxwdybablotibypmjutclgjurbmhztboqatrdwsomnxnmocvixxvfiqwmednahdqhxjkvcyhpxxdmzzuyyqdjibvmfkmonfxmohhshpkhmntnoplphqyprveyfsmsxjfosmicdsjrieeytpnbhlsziwxnpmgoxneqbnufhfwrjbqcsdfarybzwaplmxckkgclvwqdbpumsmqkswmjwnkuqbicykoisqwoootrdpdvcuiuswfqmrkctsgrevcxnyncmivsxbpbxzxpwchiwtkroqisnmrbmefbmatmdknaklpgpyqlsccgunaibsloyqpnsibwuowebomrmcegejozypjzjunjmeygozcjqbnrpakdermjcckartbcppmbtkhkmmtcngteigjnxxyzaibtdcwutkvpwezisskfaeljmxyjwykwglqlnofhycwuivdbnpintuyhtyqpwaoelgpbuwiuyeqhbvkqlsfgmeoheexbhnhutxvnvfjwlzfmvpcghiowocdsjcvqrdmkcizxnivbianfpsnzabxqecinhgfyjrjlbikrrgsbgfgyxtzzwwpayapfgueroncpxogouyrdgzdfucfrywtywjeefkvtzxlwmrniselyeodysirqflpduvibfdvedgcrzpzrunpadvawfsmmddqzaaahfxlifobffbyzqqbtlcpquedzjvykvarayfldvmkapjcfzfbmhscdwhciecsbdledspgpdtsteuafzbrjuvmsfrajtulwirzagiqjdiehefmfifocadxfuxrpsemavncdxuoaetjkavqicgndjkkfhbvbhjdcygfwcwyhpirrfjziqonbyxhibelinpllxsjzoiifscwzlyjdmwhnuovvugfhvquuleuzmehggdfubpzolgbhwyeqekzccuypaspozwuhbzbdqdtejuniuuyagackubauvriwneeqfhtwkocuipcelcfrcjcymcuktegiikyosumeioatfcxrheklookaqekljtvtdwhxsteajevpjviqzudnjnqbucnfvkybggaybebljwcstmktgnipdyrxbgewqczzkaxmeazpzbjsntltjwlmuclxirwytvxgvxscztryubtjweehapvxrguzzsatozzjytnamfyiitreyxmanhzeqwgpoikcjlokebksgkaqetverjegqgkicsyqcktmwjwakivtsxjwrgakphqincqrxqhzbcnxljzwturmsaklhnvyungjrxaonjqomdnxpnvihmwzphkyuhwqwdboabepmwgyatyrgtboiypxfavbjtrgwswyvcqhzwibpisydtmltbkydhznbsvxktyfxopwkxzbftzknnwipghuoijrbgqnzovxckvojvsqqraffwowfvqvfcmiicwitrhxdeombgesxexedlakitfovtydxunqnwqqdeeekiwjnwoshqcsljiicgobbbuqakjdonjawgjlezdnqhfdqnmsuavxdpnfzwipmspiabveaarshzwxmirgkmfncvtdrdvfxkpxlkdokxgtwcskmjryyymcthfnkasinihaunohkxaibtsqelockaefjmsuolebtnepauwmrxutspjwaxbmahsjtkfkxlnszribmeofbkyvbjscjtqjakuwvcgunvnonvqbbggfshauqsyznokqbhowjusypfnecffenojfvlblgzntqzlrgzprvhqnpfrrkzxznieiuivajivzijsqijigtatifmbplzqahuidegfoobpymkputzamzvweiyvvzlwihgmmmrcburbgbsdxrfjsbiylitghgcpqjbevvgypxcybubyoijijrhuzcdijfybqbfowlookqmlnplbxvjjosfqviygqyhgamuwzjklbyzopkrnhbywtfoqomweldmlrhjqswctubiknzzvcztyehouvnyiqnvkufaobehxhrjvtisxjlxoumipzjarwvbsaegdkpbsjmpevjbewzuqnfhoohhmdjgfpmjzdmtmykqvtucptwfidpwtwffzolffzqfdearclkyeecuzabjeqhxpmfodsvisnpxrqowdawheydfyhoexvcmihdlzavtqlshdhdgjzpozvvackebhgqppvcrvymljfvooauxcjnbejdivikcoaugxwzsulgfqdtefpehbrlhaoqxwcancuvbqutnfbuygoemditeagmcveatgaikwflozgdhkyfqmjcruyyuemwbqwxyyfiwnvlmbovlmccaoguieu"; 	const char* x = "cjgamyzjwxrgwedhsexosmswogckohesskteksqgrjonnrwhywxqkqmywqjlxnfrayykqotkzhxmbwvzstrcjfchvluvbaobymlrcgbbqaprwlsqglsrqvynitklvzmvlamqipryqjpmwhdcsxtkutyfoiqljfhxftnnjgmbpdplnuphuksoestuckgopnlwiyltezuwdmhsgzzajtrpnkkswsglhrjprxlvwftbtdtacvclotdcepuahcootzfkwqhtydwrgqrilwvbpadvpzwybmowluikmsfkvbebrxletigjjlealczoqnnejvowptikumnokysfjyoskvsxztnqhcwsamopfzablnrxokdxktrwqjvqfjimneenqvdxufahsshiemfofwlyiionrybfchuucxtyctixlpfrbngiltgtbwivujcyrwutwnuajcxwtfowuuefpnzqljnitpgkobfkqzkzdkwwpksjgzqvoplbzzjuqqgetlojnblslhpatjlzkbuathcuilqzdwfyhwkwxvpicgkxrxweaqevziriwhjzdqanmkljfatjifgaccefukavvsfrbqshhswtchfjkausgaukeapanswimbznstubmswqadckewemzbwdbogogcysfxhzreafwxxwczigwpuvqtathgkpkijqiqrzwugtr"; 	fft(y, x);  	y = "dhs5goe13j45ddsfoea"; 	x = "esd"; 	fft(y, x);  	 y = "sdjajkcfaknfjdash   "; 	 x = "anc"; 	fft(y, x);  	return 0;  }

user: user-0
content: 顶一下

user: user-0
content: 引用 10 楼 qybao 的回复:找出B每个不同字符在A的下标，并且统计B有多少个不同的字符，从第一个下标开始找，直到B的字符下标都出现，算出两个下标了间隔，取间隔最小的一组即可。  想法一致，先去重，再按这法子找，这个思路已经有java的实现了，原贴中哪一层有链接来着

user: user-2
content: 找出B每个不同字符在A的下标，并且统计B有多少个不同的字符，从第一个下标开始找，直到B的字符下标都出现，算出两个下标了间隔，取间隔最小的一组即可。

user: user-3
content: 引用 8 楼 與蘤舊縐 的回复：[quote=引用 7 楼 初学小菜 的回复:] [quote=引用 4 楼 與蘤舊縐 的回复：][quote=引用 3 楼 初学小菜 的回复:] 没有时间复杂度要求，set暴力直接做不就行了……  总得对自己有点要求，你看人家js毫秒级出解，C能虚了他[/quote]方便的话可以发出oj链接呀，搞一下不就知道那个速度更快了[/quote]  看上面发的原帖链接，不是oj[/quote]那就么得意思了……

user: user-0
content: 引用 7 楼 初学小菜 的回复:[quote=引用 4 楼 與蘤舊縐 的回复：][quote=引用 3 楼 初学小菜 的回复:] 没有时间复杂度要求，set暴力直接做不就行了……  总得对自己有点要求，你看人家js毫秒级出解，C能虚了他[/quote]方便的话可以发出oj链接呀，搞一下不就知道那个速度更快了[/quote]  看上面发的原帖链接，不是oj

user: user-3
content: 引用 4 楼 與蘤舊縐 的回复：[quote=引用 3 楼 初学小菜 的回复:] 没有时间复杂度要求，set暴力直接做不就行了……  总得对自己有点要求，你看人家js毫秒级出解，C能虚了他[/quote]方便的话可以发出oj链接呀，搞一下不就知道那个速度更快了

user: user-3
content: 引用 4 楼 與蘤舊縐 的回复：[quote=引用 3 楼 初学小菜 的回复:] 没有时间复杂度要求，set暴力直接做不就行了……  总得对自己有点要求，你看人家js毫秒级出解，C能虚了他[/quote]10的6次以下都是0ms

user: user-3
content: 引用 4 楼 與蘤舊縐 的回复：[quote=引用 3 楼 初学小菜 的回复:] 没有时间复杂度要求，set暴力直接做不就行了……  总得对自己有点要求，你看人家js毫秒级出解，C能虚了他[/quote]多大数据10的9次？

user: user-0
content: 引用 3 楼 初学小菜 的回复:没有时间复杂度要求，set暴力直接做不就行了……  总得对自己有点要求，你看人家js毫秒级出解，C能虚了他

user: user-3
content: 没有时间复杂度要求，set暴力直接做不就行了……

user: user-0
content: https://bbs.csdn.net/topics/394884533原贴找到了

user: user-4
content: 顶一下，但是不想做，今天脑子被微分方程搞炸了