

Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include <bits/stdc++.h>
using namespace std;
int countFreq(string& pat, string& txt) {
int M = pat.length();
int N = txt.length();
int res = 0;
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j]) break;
if (j == M) res++;
}
return res;
}
int main() {
string s;
string tmp = "";
cin >> s;
for (int i = 0; i < s.length(); i++) {
tmp += s[i];
if (countFreq(tmp,s)*tmp.length() == s.length()) {
cout << tmp;
break;
}
}
return 0;
}
Hãy giúp mọi người biết câu trả lời này thế nào?
![]()
#include<bits/stdc++.h>
using namespace std;
int n,i;
string s,s_temp="",s_temp1="";
int main()
{
freopen("UOCXAU.inp","r",stdin);
freopen("UOCXAU.out","w",stdout);
cin>>s;
n=s.size();
for(i=1;i<=n;i++){
if(n%i==0){
s_temp=s.substr(0,i);
s_temp1=s_temp;
for(int j=1;j<n/i;j++){
s_temp1+=s_temp;
}
if(s_temp1==s){
cout<<s_temp;
return 0;
}
}
}
}
full nhé hì hì ý tưởng là chạy for tới s.size() xong nếu như n chia hết cho i thì cắt từ đoạn 0 đến i rồi nhân lên số lần mà n/i, nếu xâu mới bằng xâu cũ thì nó là đúng.
Hãy giúp mọi người biết câu trả lời này thế nào?
![]()
Bảng tin