zoukankan      html  css  js  c++  java
  • [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher D

    D - Cyclic Nacklace HDU - 3746

    题目链接:https://vjudge.net/contest/70325#problem/D

    题目:

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task.

    As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl's fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls' lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet's cycle is 9 and its cyclic count is 2:

    Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden.
    CC is satisfied with his ideas and ask you for help.

    InputThe first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.
    Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by 'a' ~'z' characters. The length of the string Len: ( 3 <= Len <= 100000 ).OutputFor each case, you are required to output the minimum count of pearls added to make a CharmBracelet.Sample Input
    3
    aaa
    abca
    abcde
    Sample Output
    0
    2
    5

    题意:至少两个循环节,问你至少要加多少个字母使得字符串都是循环的且循环节大于等于2

    思路:利用kmp,求出next数组,其含义为子串前后缀最长相等长度,这样字符串长度-next[n]即可求出一个循环节有多少字母,当next[n]!=0且总长度%循环节字母数=0的话那么

    据不需要再加字母了,否则只要输出一个循环节总字母数-next[n]%循环节字母数,减去的那个就是最后一个不是循环节内的已经有的字母数

     1 // 
     2 // Created by HJYL on 2019/8/15.
     3 //
     4 #include <iostream>
     5 #include <vector>
     6 #include <map>
     7 #include <string>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 #include <algorithm>
    12 #include <cstdio>
    13 #include <cstring>
    14 #include <cmath>
    15 #include <cstdlib>
    16 using namespace std;
    17 const int maxn=1e6+10;
    18 char str[maxn];
    19 int nextt[maxn];
    20 void getnext()
    21 {
    22     int n=strlen(str);
    23     int i=0,j=-1;
    24     nextt[0]=-1;
    25     while(i<n)
    26     {
    27         if(j==-1||str[i]==str[j])
    28         {
    29             ++i,j++;
    30             if(str[i]!=str[j])
    31                 nextt[i]=j;
    32             else
    33                 nextt[i]=nextt[j];
    34         } else
    35             j=nextt[j];
    36 
    37     }
    38 }
    39 int main()
    40 {
    41     //freopen("C:\Users\asus567767\CLionProjects\untitled\text","r",stdin);
    42     int T;
    43     scanf("%d",&T);
    44     while(T--)
    45     {
    46         scanf("%s",str);
    47         getnext();
    48         int n=strlen(str);
    49         int len=n-nextt[n];
    50         if(nextt[n]!=0&&n%len==0)
    51             printf("0
    ");
    52         else
    53             printf("%d
    ",len-nextt[n]%len);
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    C#创建线程
    Halcon算子
    二叉树的层次遍历
    反转单链表
    “开-闭”原则 (Open-Closed principle, OCP)
    CSUOJ1867 John and Health rate
    LOCAL_MODULE_TAGS
    void * kmalloc(size_t size, int flags)
    printk(Loglevels string)
    container_of宏定义解析
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11358563.html
Copyright © 2011-2022 走看看