#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;
}

1 comments

  • @ 2024-4-5 20:40:06

    标程使用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";
      }
    }
    
    • 1

    Information

    ID
    3
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    (None)
    # Submissions
    47
    Accepted
    2
    Uploaded By