返回题目问答
讨论 / 题目问答/ 帖子详情

80分,不知道错误在哪。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool check(string str,string temp){
    string str1=str;
    int a=str.size();
    int b=temp.size();
    for(int i=0;i<b/a-1;++i)
        str+=str1;
    if(str==temp){
        cout<<str1<<endl<<b/a<<endl;
        return true;
    }
    return false;
}
int main(){
    int n;
    cin>>n;
    vector<string> nums(n,"");
    for(int i=0;i<n;++i)
        cin>>nums[i];
    for(int i=0;i<n;++i){
        string str="";
       for(int j=0;j<nums[i].size();++j){
        str+=nums[i][j];
        if(check(str,nums[i])){
            break;
        }
       }
    }
    return 0;
}
cpp
1 回复 0 转发 0 喜欢 146 阅读
回复 (1)
默认 最新
admin 2024/4/5
标程使用KMP的性质

#include <bits/stdc++.h>
using namespace std;
char a[1000005];
int nex[1000005];
int main()
{
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int t;
  cin>>t;
  while(t--)
  {
    cin>>a+1;
    int j=0;
    int len=strlen(a+1);
    memset(nex,0,sizeof(nex));
    for(int i=2;i<=len;i++)
    {
      while(j&&a[i]!=a[j+1])
      j=nex[j];
      if(a[i]==a[j+1])
      j++;
      nex[i]=j;
    }
    int jk=len-nex[len];
    for(int i=1;i<=jk;i++)
    cout<<a[i];
    cout<<"\n"<<len/jk<<"\n";
  }
}
0