HDU-2609-how many

Source: Internet
Author: User

Start with the question:

How many

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1330 accepted submission (s): 523


Problem descriptiongive you n (n <10000) necklaces, the length of neck?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 neck.pdf, 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 input401101100100100111000010010110000001 sample output12 question: give n strings with only 0 and 1, and then ask how many different strings are there. Practice: first obtain the minimum representation of each string, and then compare the minimum representation of different strings to obtain the result. For more information about the minimum representation of strings, here is my understanding. First look at the Code:
 1 char e[102]; 2 int st,k; 3  4 void setMin() 5 { 6     k=strlen(e); 7     int i=0,j=1,l=0,d; 8     while(i<k && j<k && l<k) 9     {10         d=e[(i+l)%k]-e[(j+l)%k];11         if(d==0) l++;12         else13         {14             if(d>0) i=i+l+1;15             else j=j+l+1;16             if(i==j) j++;17             l=0;18         }19     }20     st=min(i,j);21 }

Let's talk about the simple algorithm: Start With enumeration, and then compare the strings starting with these to find the smallest one. The complexity is (String Length * string length ).

Then let's talk about the optimized algorithm: we start with two strings labeled as 0 (I) and subscript 1 (j) as the starting point. Then compare, when we find e [(I + l) % K]! = E [(j + l) % K], we will move the pointer from the large side to the position (x + L + 1, this skips redundant comparisons. Why is this true? The explanation of this part cannot be clearly stated.

/* This is just like.... It hasn't been written yet.

If E [I '''] <E [J '''] & I <j, J will jump to the next position, if the current I position is not the smallest position, it is possible to jump to the current J ~ during future comparison ~ If you do not jump to the position where J + L + 1 is located, it indicates that the character string starting from the position of J is smaller.

*/

 

 

Code:

 

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define min(x,y) (x < y ? x : y) 5 #define MAX 10002 6 using namespace std; 7  8 typedef struct str{ 9     char c[102];10     bool operator < (const str& o)const{11         return strcmp(c,o.c)<0;12     }13 }str;14 str ss[MAX];15 16 char e[102];17 int st,k;18 19 void setMin()20 {21     k=strlen(e);22     int i=0,j=1,l=0,d;23     while(i<k && j<k && l<k)24     {25         d=e[(i+l)%k]-e[(j+l)%k];26         if(d==0) l++;27         else28         {29             if(d>0) i=i+l+1;30             else j=j+l+1;31             if(i==j) j++;32             l=0;33         }34     }35     st=min(i,j);36 }37 38 int main()39 {40     int n;41     while(scanf("%d",&n)!=EOF)42     {43         for(int i=0; i<n; i++)44         {45             scanf("%s",e);46             setMin();47             for(int j=0;j<k;j++){48                 ss[i].c[j]=e[(st+j)%k];49             }50             ss[i].c[k]=‘\0‘;51         }52         sort(ss,ss+n);53         int sum=1;54         for(int i=1;i<n;i++){55             if(strcmp(ss[i].c,ss[i-1].c)!=0) sum++;56         }57         printf("%d\n",sum);58     }59     return 0;60 }
/X 2609 */

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.