(Incomplete) UVa 10298 Power Strings

方法:暴力 / kmp

先说一下暴力的方法,就是尝试把原来的string不断分割,在substring 里求解。code 如下

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#include <iomanip>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline ‘\n‘

#define test if(1)if(0)cerr
using namespace std;
  using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string str;
    while (cin >> str)
    {
        if (str == ".") break;
        int n, m;
        n = m = str.length();
        for (int i = 2; i <= n; ++i)
        {
            while (n % i == 0)
            {
                n /= i;
                int j = 0;
                int len = m/i;
                while (j < m-len && str[j] == str[j+len]) ++j;
                if (j == m-m/i)
                {
                    m = len;
                }
                else
                    break;
            }
            //cerr << i << " " << m << newline;
            while (n % i == 0) n /= i;
        }
        cout << str.length()/m << newline;
    }
}

  

然而看到题解,好像是用kmp,值得学习

http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html

时间: 2024-12-26 13:42:58

(Incomplete) UVa 10298 Power Strings的相关文章

UVa 10298 - Power Strings

题目:求一个串的最大的循环次数. 分析:dp,KMP,字符串.这里利用KMP算法. KMP的next函数是跳跃到最近的串的递归结构位置(串元素取值0 ~ len-1): 由KMP过程可知: 如果存在循环节,则S[0 ~ next[len]-1] 与 S[len-next[len] ~ len-1]相匹配: 则S[next[len] ~ len-1]就是循环节(且最小),否则next[len]为0: 因此,最大循环次数为len/(len-next[len]),最小循环节为S[next[len] ~

UVA - 10298 Power Strings (KMP求字符串循环节)

Description Problem D: Power Strings 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, exponentiati

UVA 10298 - Power Strings(KMP)

UVA 10298 - Power Strings 题目链接 题意:本意其实就是,给定一个字符串,求出最小循环节需要几次循环出原字符串 思路:利用KMP中next数组的性质,n - next[n]就是最小循环节,然后n / 循环节就是答案 代码: #include <cstdio> #include <cstring> const int N = 1000005; char str[N]; int next[N]; void getnext() { int n = strlen(s

UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

题意:定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路:如果完全不循环,顶多就是类似于abc1这样咯,即n=1.但是如果循环出现了,比如abab,那就可以表示成(ab)2.还有一点,就是要使得n尽量大,那么当出现abababab时,应该要这么表示(ab)4,而不是(abab)2. 此题用神奇的KMP解决,也就是主要利用next数组.举例说明. 一般出现循环的都会大概是这样的:abcabcabc.而这样

(Incomplete)UVa 701 The Archeologist&#39;s Dilemma

方法:对数,暴力 我们需要求出最小的e,是的存在一个i > len(n) , 满足   floor[2^e/ (10^i)]= n, 即 n*10^i < 2^e < (n+1)*10^i.对两边同时取log10 (以10为底的对数,记作lg),得到 lg(n) + i < e*lg(2) < lg(n+1) + i. 注意len(n) = (int) lg(n)+1, 之前一个条件 i > len(n) 可以转化为 i > lg(n)+1,  然后暴力枚举即可.

Power Strings(KMP)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45008   Accepted: 18794 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 = "

POJ 2406 Power Strings

F - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2406 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = &

G - Power Strings POJ 2406 (字符串的周期)

G - Power Strings Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2406 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def

Power Strings (poj 2406 KMP)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   Accepted: 13804 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def"