[转]浅析“最小表示法”思想在字符串循环同构问题中的应用-HDU2609

来源:互联网 发布:java 9 发布 编辑:程序博客网 时间:2024/05/29 19:23

ACM偶尔会出现最小表示法的题目,下面是一些文档可以帮助理解。

下载地址

How many

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

Problem Description
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.
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
4011011001001001141010010110000001
Sample Output
12
Author
yifenfei
Source
奋斗的年代
Recommend
yifenfei   |   We have carefully selected several similar problems for you:  2612 2572 1325 2610 1686 
 
解释:见上面的pdf。Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor97690422013-12-06 16:16:07Accepted2609109MS2032K866 BC++ 
//第一次用最小表示法#include<iostream>#include<set>#include<string>#include<math.h>using namespace std;int GetMin(const string& src){int i=0,j=1,k=0,len=src.size(),pos;while (i < len && j < len && k < len){pos = src[(i+k)%len]-src[(j+k)%len];if( pos == 0 )++k;else{if( pos > 0 )i = i + k + 1;elsej = j + k + 1;if( i == j )++j;k = 0;}}return i < j ?  i : j;}int main(){int T,pos,diff;string src,strTemp;set<string> Set;while(cin >> T){Set.clear();diff = 0;while( T-- ){cin >> strTemp;pos = GetMin(strTemp);src.assign(strTemp,pos,strTemp.size()-pos);if( pos )src.append(strTemp,0,pos);if( Set.count(src) == 0 ){++diff;Set.insert(src);}}cout << diff << endl;}return 0;}