字符串中重复出现的最长的子字符串【source:程序员面试宝典p238】

#include <iostream>
#include <string>
using namespace std;

void func(string& str) {
	string tep;

	for(int i=str.length()-1; i>1; --i) {
		for(int j=0; j<str.length(); ++j) {
			if(j+i <= str.length()) {
				size_t index = 0;
				size_t rindex = 0;
				tep = str.substr(j, i);
//				cout << tep << endl;

				index = str.find(tep);
				rindex = str.rfind(tep);
				if(index != rindex) {
					cout << tep << " " << index + 1<<endl;
					return;
				}
			}
		}
	}
}

int main() {
	string str;
	while(cin >> str)
		func(str);

	return 0;
}



你可能感兴趣的:(重复子串,最长)