【玲珑杯 1054】【暴力+枚举约数】String cut

传送门:http://www.ifrog.cc/acm/problem/1054

思路:

首先知道这样一个性质,删除一个字符之后的字符串的循环节长度一定是nn的约数。所有我们就枚举删除哪一个字符,然后对每一个约数判断是否可以循环节为它。复杂度O(nlogn)


代码:

#include <iostream>
#include <stdio.h> 
#include <algorithm>
#include <string.h>
using  namespace  std;

#define rep(i,k,n) for(int i=k;i<=n;i++)
#define rrep(i,k,n) for(int i=k;i>=n;i--)
#define pl(x) cout << #x << "= " << x << endl;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
const int N=1e5+10;
string s;

int  main(){
  int T;
  read(T);
  while(T--){
    int n;
    read(n);
    // if(n==1) return 0*puts(0);
    cin>>s;
    int mx=0;
    rep(i, 0, n-1){
      string ss = s.substr(0, i);
      ss = ss+s.substr(i+1, n-i);
      // cout<<"ss="<<ss<<endl;
      // pl(i);
      for(int d = 1; d<=n-1; d++){
        if((n-1)%d==0)
        {//这个条件不能乱放到循环里面
          if((n-1)/d<=mx)break;
          bool flag = true;
          string sub = ss.substr(0, d);
          for(int j=0; j<=n-2; j+=d)
          {
            string sb = ss.substr(j, d);
            // pl(sb);
            if(sb != sub)
            {
              flag=false;
              break;
            }
          }
          if(flag)
          {
            mx = max(mx, (n-1)/d);
          }
        }
      }
    }
    printf("%d\n", mx);
  }
  return 0;
}


描述:

1054 - String cut

Time Limit:4s Memory Limit:64MByte

Submissions:151Solved:54

DESCRIPTION

A string cut cc means the string can be divided into cc same substring. Such as string: abababababab.Because it can be divied into abab + abab + abab, the string cut of it is 33.
Give you a string strstr.you need to delete one char of it ,and after your operation the string cut of it will be biggest.Print the biggest string cut.

INPUT
There are multiple test cases.The first line is a number T ( T 10T ≤10), which means the number of cases.For each case, a line has a integer n(1<=n<=100001)n(1<=n<=100001),which is the length of string.next line a string str str (which just include lowercase).
OUTPUT
one line --- the biggest string cut.
SAMPLE INPUT
23aab5aabaa
SAMPLE OUTPUT
24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值