zoukankan      html  css  js  c++  java
  • kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me
    How many kinds of necklaces total have.(if two necklaces can equal by rotating ,we say the two necklaces are some).
    For example 0110 express a necklace, you can rotate it. 0110 -> 1100 -> 1001 -> 0011->0110.

    InputThe input contains multiple test cases.
    Each test case include: first one integers n. (2<=n<=10000)
    Next n lines follow. Each line has a equal length character string. (string only include '0','1').
    OutputFor each test case output a integer , how many different necklaces.Sample Input
    4
    0110
    1100
    1001
    0011
    4
    1010
    0101
    1000
    0001
    Sample Output
    1
    2


    本来是各种找特征,然后扩展kmp判,但是找不到,看了题解。学习了最小字符串

    s=“bbaa” 经过循环能够得到4个同构字符串, 其中最小的是 “aabb”

    如何求最小字符串

    i=0, j=1, k=0

    如果 s[i] < s[j] 很容易理解 j++;
    如果 s[i] > s[j] 也很好理解 i=j;
    如果 s[i] == s[j] ,
    可以令 k=0, 在i和j之间 找到 第一个s[i+k] != s[j+k]的位置
    如果 s[i+k] < s[j+k] 说明i~i+k 都符合,所以 j=j+k+1
    如果 s[i+k] > s[j+k] 说明i-i+k 都不符合, 所以 i=i+k+1

    两个注意事项:
    第一: i和j不能相等
    第二: 每次s[i] != s[j] ,k=0

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<string>
     4 #include<set>
     5 #include<algorithm>
     6 using namespace std;
     7 set<string> ss;
     8 int n,len;
     9 char s[210],t[210];
    10 
    11 //最小字符串模板
    12 int minstring(char* s) {
    13     int i=0,j=1,k=0;
    14     while(i<len&&j<len&&k<len) {
    15         int tmp=s[(i+k)%len]-s[(j+k)%len];
    16         if(!tmp) k++;
    17         else {
    18             if(tmp<0) {
    19                 j+=k+1;
    20             } else {
    21                 i+=k+1;
    22             }
    23             if(i==j) j++;
    24             k=0;
    25         }
    26     }
    27     return min(i,j);
    28 }
    29 
    30 void getstring(char* str) {//写法很厉害
    31     str[len/2]='';
    32     ss.insert(str); //竟然还能这样
    33 }
    34 
    35 int main() {
    36     while(~scanf("%d",&n)) {
    37         for(int i=0;i<n;i++) {
    38             scanf("%s",t);
    39             strcpy(s,t);
    40             strcat(s,t);
    41             len=strlen(s);
    42             int k=minstring(s);//得到最小字符串的起始位置
    43             getstring(s+k);
    44         }
    45         printf("%d
    ",ss.size());
    46         ss.clear();
    47     }
    48 }
  • 相关阅读:
    MySQL8.0设置远程访问权限
    MySQL创建用户与授权
    input lable水平对齐
    jquery datagrid加载后仅选定第一行
    jquery隐藏按钮
    C# 添加excel批注
    在ashx中使用Server对象
    jquery 获取datagrid行数
    弹出窗体中加载页面
    【MySQL】MySQL零碎积累
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/10320649.html
Copyright © 2011-2022 走看看