2018牛客多校 Sort String

链接:https://www.nowcoder.com/acm/contest/141/E
来源:牛客网
 

Eddy likes to play with string which is a sequence of characters. One day, Eddy has played with a string S for a long time and wonders how could make it more enjoyable. Eddy comes up with following procedure:

1. For each i in [0,|S|-1], let Si be the substring of S starting from i-th character to the end followed by the substring of first i characters of S. Index of string starts from 0.
2. Group up all the Si. Si and Sj will be the same group if and only if Si=Sj.
3. For each group, let Lj be the list of index i in non-decreasing order of Si in this group.
4. Sort all the Lj by lexicographical order.

Eddy can't find any efficient way to compute the final result. As one of his best friend, you come to help him compute the answer!

输入描述:

Input contains only one line consisting of a string S.

1≤ |S|≤ 106
S only contains lowercase English letters(i.e. ).

输出描述:

First, output one line containing an integer K indicating the number of lists.
For each following K lines, output each list in lexicographical order.
For each list, output its length followed by the indexes in it separated by a single space.

示例1

输入

abab

输出

2
2 0 2
2 1 3

示例2

输入

deadbeef

输出

8
1 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7

题意挺长的:

根据原字符串构造新串,对于原字符串从0~|S|-1,i从0开始,从i到最后的子串放到从0到i-1子串的前面。

对于这些新构造的子串,从0开始编号,将相同的新字符串们归为一组,

输出一共有多少组,每组有多少个新字符串,以及新字符串的编号。  按字典序输出

 思路:

  首先多写几个字符串

会发现只有字符串存在循环子串情况下,才会出现相同的字符串,才会出现多个字符串同组情况。

利用kmp求出失配数组next[i],表示前i个字符串最长前缀和后缀的长度。

求出循环子串的长度,根据循环子串判断并输出分组情况。

#include<bits/stdc++.h>
using namespace std;

const int maxn =1e6+10;
int next1[maxn];
char s1[maxn];

void kmp_pre(char x[],int m,int next1[])
{
    int i,j;
    j=next1[0]=-1;
    i=0;
    while(i<m){
        while(-1!=j&&x[i]!=x[j])j=next1[j];
        next1[++i]=++j;
    }
}
int main()
{
        scanf("%s",s1);
        int len=strlen(s1);
        kmp_pre(s1,len,next1);
        int temp=len-next1[len];
        if(len%temp!=0)
        {
        temp=len;
        printf("%d\n",temp);
        for(int i=0;i<temp;i++)
           {
             printf("%d %d",len/temp,i);
             for(int j=1;j<len/temp;j++)
                printf(" %d",i+j*temp);
             printf("\n");
           }
        }
        else{
             printf("%d\n",temp);
        for(int i=0;i<temp;i++)
           {
             printf("%d %d",len/temp,i);
             for(int j=1;j<len/temp;j++)
                printf(" %d",i+j*temp);
             printf("\n");
           }
        }
    return 0;
}

### 联合活动 Chocolate 问题解题思路 #### 背景描述 在网的联合活动中,“Chocolate”问题是关于如何合理分配巧克力给朋友。具体来说,目标是通过K次切割将一块由个连续部分组成的巧克力分成K+1份,每一份都包含一些连续的部分。 #### 解决方案概述 为了有效地解决这个问题,可以采用动态规划的方法来寻找最优解法。该方法的核心在于定义状态转移方程以及初始化边界条件[^1]。 #### 动态规划实现细节 - **状态表示**:设`dp[i][j]`代表前i个块中做最j刀所能获得的最大价值。 - **初始条件**:当没有切分时(`j=0`),最大值即为整个区间的总和;对于其他情况,则需遍历所有可能的位置进行尝试。 - **状态转移**:对于每一个新的位置k,在其之前已经完成了一定数量的分割操作(j),此时需要计算从当前位置到起点之间的最小成本,并更新全局最优解。 ```python def max_chocolate_value(chunks, K): n = len(chunks) # 计算区间内的累积和用于快速求子数组之和 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + chunks[i - 1] dp = [[float('-inf')] * (K + 1) for _ in range(n)] # 初始化第一列 for i in range(n): dp[i][0] = prefix_sum[i + 1] for j in range(K + 1): # 遍历每一刀数目的可能性 for i in range(n): # 当前考虑到第几个chunk为止 if j == 0 or i < j: continue for p in range(i): # 尝试不同的最后一刀位置p cost = abs(prefix_sum[p + 1] - prefix_sum[i + 1]) dp[i][j] = max(dp[i][j], min(dp[p][j - 1], cost)) return dp[-1][-1] ``` 此算法的时间复杂度主要取决于三重循环结构O(N^3*K),其中N为chunks的数量,K为允许的最大切割次数。虽然看起来效率不高但对于题目规模而言是可以接受的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值