POJ 2406 - Power String

本文介绍了一种通过分析输入字符串来确定其是否为周期性重复的算法,并计算出构成该字符串的基本子串的重复次数。利用模式匹配的思想,通过预处理获取最长公共前后缀的长度,进而判断并计算出循环次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.


Sample Output

1
4
3


题意:给出一个周期字符串,求出其中子字符串循环的次数。

把第一个子字符串作为模式,求出长度,再进行计算即可。

#include <cstdio>
#include <cstring>

char str[1000005];
int tar[1000005];

int main()
{
    while (scanf("%s", str) != EOF)
    {
        if (strcmp(str, ".") == 0)
            break;
        int len = strlen(str);
        tar[0] = 0;

        int cur = -1;
        for (int i = 1; i < len; ++i)
        {
            while (cur > 0 && str[cur+1] != str[i])
                cur = -1;
            if (str[cur+1] == str[i])
                cur++;
            tar[i] = cur;
        }
        if (len % (len - tar[len - 1] -1) == 0)
            printf("%d\n", len / (len - tar[len - 1] - 1));
        else
            printf("1\n");
    }
    return 0;
}


在Java中,你可以使用动态规划的方法解决POJ 3233矩阵幂序列的问题,这是一个关于快速幂运算的实际应用。这个问题通常涉及到计算给定矩阵的幂,直到达到某个特定的幂次。下面是一个简单的实现示例: ```java import java.util.Scanner; public class MatrixPower { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // 矩阵的大小 int m = scanner.nextInt(); // 目标幂次 int[][] matrix = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = scanner.nextInt(); } } int[][] result = matrixPow(matrix, m); // 计算矩阵的m次方 // 打印结果矩阵 for (int[] row : result) { for (int num : row) { System.out.print(num + " "); } System.out.println(); } } private static int[][] matrixPow(int[][] matrix, int power) { if (power == 1) { return matrix; } else if (power % 2 == 0) { // 如果是偶数,先平方再除半 int[][] square = matrixPow(matrix, power / 2); return multiplyMatrices(square, square); } else { // 如果是奇数,先平方然后乘以原矩阵 int[][] square = matrixPow(matrix, power / 2); return multiplyMatrices(multiplyMatrices(square, square), matrix); } } // 用于矩阵相乘 private static int[][] multiplyMatrices(int[][] a, int[][] b) { int[][] result = new int[a.length][b[0].length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < b[0].length; j++) { for (int k = 0; k < a[0].length; k++) { result[i][j] += a[i][k] * b[k][j]; } } } return result; } } ``` 实现逻辑解释: 1. `matrixPow`函数采用分治策略,通过递归地将幂次数分为一半一半,直至最后变成基础情况(即1次幂),然后逐步构建出最终结果。 2. 当幂是偶数时,我们先计算矩阵的一半的幂,然后通过矩阵乘法得到原始矩阵的平方,再除以2。 3. 当幂是奇数时,同样先计算矩阵的一半的幂,然后将这个平方矩阵自乘一次,最后乘以原始矩阵。 4. `multiplyMatrices`函数实现了两个矩阵的乘法操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值