给出一个非空的字符串,判断这个字符串是否是由它的一个子串进行多次首尾拼接构成的。
例如,"abcabcabc"满足条件,因为它是由"abc"首尾拼接而成的,而"abcab"则不满足条件。
//利用KMP算法 #include <iostream> #include <string> #include <vector> using namespace std; class Solution { public: string& repeatedSubstringPattern(string s) { int l = s.length(); vector<int> next(l); string res; next[0] = -1; int i, j = -1; for (i = 1; i < l; i++) { while (j >= 0 && s[i] != s[j + 1]) { j = next[j]; } if (s[i] == s[j + 1]) { j++; } next[i] = j; } int lenSub = l - 1 - next[l - 1]; if(lenSub != l && l % lenSub ==0) { for(int i=0;i<lenSub;++i) res += s[i]; } return res; } }; int main() { string str; while (cin >> str) { cout<<Solution().repeatedSubstringPattern(str); } return 0; }
#include <iostream> #include <string> using namespace std; string process(const string & str) { for (int i = 2; i <= str.size(); ++i) { if (str.size() % i == 0) { int sub_len = str.size() / i; string sub_str = str.substr(0, sub_len); // 要求各字串都相等 int j = 0; for (j = sub_len; j < str.size(); j += sub_len) { if (sub_str != str.substr(j, sub_len)) { break; } } if (j >= str.size()) { return sub_str; } } } return ""; } int main() { string str; while (cin >> str) { string res = process(str); if (res.size() < 1) { cout << "false" << endl; } else { cout << res << endl; } } return 0; }
def main(): s = input("").strip() return find(s) def find(s): if len(s) == 1: return 'false' else: #等分份数 flag = 2 while(flag<=len(s)): #整数除,向下取整,方便截取tmp n = len(s)//flag tmp = s[0:n] #判断拼接后字符串是否等于原字符串 if tmp*flag == s: return tmp else: flag += 1 return 'false' if __name__ == '__main__': print(main())
def copy(s): if len(s)<2: return s res = [] for i in range(1,len(s)//2+1): ss = s[:i] if is_copy(ss,s): res.append(ss) return res if res else 'false' def is_copy(ss,s): i = len(s)//len(ss) if len(s)%len(ss) != 0: return False if ss*i == s: return True else: return False s = input() print(copy(s))
def judge(s): l = len(s) for i in range(1, l / 2 + 1): if l % i == 0: substr_len = l / i sub_str = s[:i] for k in range(1, substr_len+1): if sub_str != s[k*i : i*(k+1)]: break if k == substr_len : return s[:i] return 'false' if __name__ == '__main__': raw_str = raw_input() print (judge(raw_str))
import java.util.Scanner;
/**
* 字符串是否由子串拼接
* 给出一个非空的字符串,判断这个字符串是否是由它的一个子串进行多次首尾拼接构成的。
* 例如,"abcabcabc"满足条件,因为它是由"abc"首尾拼接而成的,而"abcab"则不满足条件。
* 输入描述:
* 非空字符串
* 输出描述:
* 如果字符串满足上述条件,则输出最长的满足条件的的子串;如果不满足条件,则输出false。
* 输入例子1:
* abcabc
* 输出例子1:
* abc
*
* @author shijiacheng
*/
public class StringIsMakeUpSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
int length = str.length();
String result = "";
for (int i = 1; i <= length / 2; i++) {
if (length % 1 == 0) {
String sub = str.substring(0, i);
String temp = "";
for (int j = 1; j <= length / i; j++) {
temp += sub;
}
if (temp.equals(str)) {
result = sub;
}
}
}
if (result.equals("")) {
System.out.println(false);
} else {
System.out.println(result);
}
}
}
var str = readline(); var len = str.length; var mid = parseInt((len - 1)/2); var result = false; for (var i = mid; i >= 0; i--) { if (len%(i+1) == 0){ var num = len/(i+1); var subStr = str.slice(0,i+1); if (subStr.repeat(num) === str) { result = subStr; break; } } } print(result);
剑指 剑指 Offer 14- I. 剪绳子(C++) 贪心算法
#include <iostream> using namespace std; bool foo(const string &str, int n) { int L = str.size(); for (int i = 0; i < L; i++) { if(str[i] != str[(i+n)%L])return false; } return true; } int main() { string s; cin >> s; int L = s.size(); int n = L / 2; int i = 1; for (; i <= n; i++) { if(L % i == 0 && foo(s, i)) { cout << s.substr(0,i); break; } } if (i > n)cout << "false"; return 0; }
#include<iostream> (720)#include<string> #include<stdlib.h> using namespace std; string find(const string& str) { string final_str = ""; string sub_str; for (int i = 1; i < str.size(); i++) { if (str.size() % i != 0) continue; sub_str = str.substr(0, i); string temp_str; for (int times = 1; times <= str.size() / i; times++) { temp_str += sub_str; } if (temp_str != str) continue; if (i >= final_str.size()) { final_str = sub_str; } } return final_str; } int main() { string str; string res; while (cin >> str) { res = find(str); if (res.size() < 1) cout << "false" << endl; else cout << res << endl; } system("PAUSE"); return 0; }
/* 遍历法,遍历子串长度从1到n/2,选择满足要求的最大子串。 */ #include<iostream> #include <string> using namespace std; int main() { string s; getline(cin,s); string res = ""; for(int i = 0; i<s.length()/2; i++) { //子串的长度应该可以被字符串长度整除。 if(s.length()%(i+1) == 0) { int real = 1; for(int j = i+1; j<s.length();j++) { int temp = ((j+1)%(i+1)-1<0)?i:((j+1)%(i+1)-1) ; if(s[temp] != s[j]) { real = 0; break; } } if(real == 1) res = s.substr(0,i+1); } } if(res == "") cout<<"false"<<endl; else cout<<res<<endl; return 0; }
#include <iostream> #include <algorithm> #include <cstdio> #include <string> using namespace std; bool fun(string out, string s) { int out_len = out.length(); int s_len = s.length(); int pos = out.find(s); if (pos != 0) return false; for (int i = 0; i < out_len; i++) { if (i%s_len == 0) { string tmp = out.substr(i); int tmp_pos = tmp.find(s); if (tmp_pos != 0) { return false; } } } return true; } string min_str(string s,int k) { int s_len = s.length(); string min = s.substr(0,1); for (int i = 1; i < s_len; i++) { min = s.substr(0, i); int tmp_pos = min.find(s[i]); if (tmp_pos == 0)break; } string new_s = min; if (k == 1) return new_s; for (int i = 0; i < k-1; i++) { new_s = new_s + min; } return new_s; } int main() { string s; cin >> s; int k = 1; string res = "false"; while (true) { string mins = min_str(s,k); k += 1; if (mins.length() > s.length()/2) break; bool match = fun(s, mins); if (match == true) res = mins; else continue; } cout << res << endl; system("pause"); return 0; }
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
boolean flag = false;
for(int i = s.length()/2;i>0;i--) {
if(s.length()%i != 0) continue;
else if(compare(s,i)) {
flag = true;
System.out.println(s.substring(0,i));
break;
}
}
if(!flag) System.out.println("false");
}
public static boolean compare(String s,int n) {
String str = s.substring(0,n);
for(int i = n;i<s.length();i+=n) {
if(!s.substring(i,i+n).equals(str)) return false;
}
return true;
}
}