hdu 2609 How many

How many

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3553    Accepted Submission(s): 1592

Problem Description

Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell meHow 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.

Input

The 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').

Output

For 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

minimal representation of a string

Let S=bcad, and S' is a cyclic isomorphic string of S. S' can be bcad or cadb, adbc, dbca. And the smallest represented S' is adbc.

For the minimum representation of string cyclic isomorphism, the essence of the problem is to find a position of the S string, and from this position, the loop outputs S, and the obtained S' lexicographical order is the smallest.

Idea: Find the bottom of each string and add it to the set container (repeated ones can be removed), and finally output the number of elements in the set container.

//Minimum representation of the string and save it in the set
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<algorithm>
using namespace std;

const int N=205;
int n, len;
set<string>v;

char s[N],t[N];

int min_Represstation(char *s)
{
	int i=0,j=1,k=0;
	while(i<len&&j<len&&k<len){
		int tmp=s[(i+k)%len]-s[(j+k)%len];
		if(tmp==0)
		k++;
		else{
			if(tmp>0)
			i+=k+1;
			else
			j+=k+1;
			if(i==j)
				j++;
			k=0;
		}
	}
	return min(i,j);
}

void getMin(char *str){
	str [len / 2] = '\ 0';
	v.insert(str);
}

intmain()
{
	while(~scanf("%d",&n))
	{
		v.clear();
		for(int i=0;i<n;i++)
		{
			scanf("%s",t);
			strcpy(s,t);
			strcat(s,t);
			len = strlen (s);
			int k=min_Represstation(s);
			getMin (s + k);
		}
		printf("%d\n",v.size());
	}
	return 0;
}

C++'s string holds the minimum representation + sorting of each string

#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string s[11000];
string minmize(string ss)
{
	int t, len;
	string mstr,a;
	len=ss.length(),t=len;
	mstr=ss;
	while(t--)
	{
		a=ss[0];
		ss=ss.substr(1);//Fetch substring function, starting from the second character of the string to the end
		ss+=a;
		if(mstr>ss)
		mstr=ss;
	}
	return mstr;
}
intmain()
{
	ios::sync_with_stdio(false);//Speed ​​up cin, cout  
	cin.tie(0);  
	int n;
	string si;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			cin >> si;
			s[i]=minmize(si);
		}
		sort(s,s+n);
		int cnt=1;
		for(int i=0;i<n-1;i++)
		if(s[i]!=s[i+1])
		cnt++;
		cout<<cnt<<endl;
	}
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325556186&siteId=291194637