ZOJ - 1729(最小表示法。 <<然后输出字典序最小
2017-07-15 11:54
225 查看
传送门
Hidden Password
Time Limit: 2 Seconds Memory Limit: 65536 KB
Some time the programmers have very strange ways to hide their passwords. See for example how Billy "Hacker" Geits hide his password. Billy chooses a string S composed of small Latin
letters with length L. Then he makes all L-1 one-letter left cyclic shifts of the string and takes as a password one prefix of the lexicographically first of the obtained strings (including S). For example let consider the string alabala. The cyclic one-letter
left shifts (including the initial string) are:
alabala
labalaa
abalaal
balaala
alaalab
laalaba
aalabal
and lexicographically first of them is the string aalabal. The first letter of this string is in position 6 in the initial string (the positions in the string are counted from 0).
Write a program that for given string S finds the start position of the smallest lexicographically one-letter left cyclic shift of this string. If the smallest lexicographically left
shift appears more than once then the program have to output the smallest initial position.
Your program has to be ready to solve more than one test case. The first line of the input file will contains only the number T of the test cases. Each of the following T lines will describe
one test case - first the length L of the string (5 <= L <= 100000) and then, separated by one space, the string S itself.
The output file have to contain exactly T lines with a single number each - the initial position found by your program.
Sample Input
2
6 baabaa
7 alabala
Sample Output
1
6
最小表示法
题意:
输出每个字符串的最小字典序字串的下标!
暴力:
Hidden Password
Time Limit: 2 Seconds Memory Limit: 65536 KB
Some time the programmers have very strange ways to hide their passwords. See for example how Billy "Hacker" Geits hide his password. Billy chooses a string S composed of small Latin
letters with length L. Then he makes all L-1 one-letter left cyclic shifts of the string and takes as a password one prefix of the lexicographically first of the obtained strings (including S). For example let consider the string alabala. The cyclic one-letter
left shifts (including the initial string) are:
alabala
labalaa
abalaal
balaala
alaalab
laalaba
aalabal
and lexicographically first of them is the string aalabal. The first letter of this string is in position 6 in the initial string (the positions in the string are counted from 0).
Write a program that for given string S finds the start position of the smallest lexicographically one-letter left cyclic shift of this string. If the smallest lexicographically left
shift appears more than once then the program have to output the smallest initial position.
Your program has to be ready to solve more than one test case. The first line of the input file will contains only the number T of the test cases. Each of the following T lines will describe
one test case - first the length L of the string (5 <= L <= 100000) and then, separated by one space, the string S itself.
The output file have to contain exactly T lines with a single number each - the initial position found by your program.
Sample Input
2
6 baabaa
7 alabala
Sample Output
1
6
最小表示法
题意:
输出每个字符串的最小字典序字串的下标!
暴力:
//china no.1 #include <vector> #include <iostream> #include <string> #include <map> #include <stack> #include <cstring> #include <queue> #include <list> #include <stdio.h> #include <set> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <cctype> #include <sstream> #include <functional> using namespace std; #define pi acos(-1) #define endl '\n' #define srand() srand(time(0)); #define me(x) memset(x,0,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); typedef long long LL; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; //const int dx[]={-1,0,1,0,-1,-1,1,1}; //const int dy[]={0,1,0,-1,1,-1,1,-1}; const int maxn=1e3+5; const int maxx=1e5+100; const double EPS=1e-7; const int MOD=10000007; #define mod(x) ((x)%MOD); template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} #define FOR(x,n,i) for(int i=x;i<=n;i++) #define FOr(x,n,i) for(int i=x;i<n;i++) #define W while #define sgn(x) ((x) < 0 ? -1 : (x) > 0) inline int Scan() { int res=0,ch,flag=0; if((ch=getchar())=='-')flag=1; else if(ch>='0' && ch<='9')res=ch-'0'; while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0'; return flag ? -res : res; } int main() { int t; close(); W(cin>>t) { W(t--) { int len; string s; cin>>len; cin>>s; //cout<<s<<endl; string S=s; int ans=0; FOR(1,len,i) { char op=s[0]; s.erase(s.begin()); s+=op; // cout<<s<<endl; if(S>s) { S=s; ans=i; } } cout<<ans<<endl; } } }
//china no.1 #include <vector> #include <iostream> #include <string> #include <map> #include <stack> #include <cstring> #include <queue> #include <list> #include <stdio.h> #include <set> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <cctype> #include <sstream> #include <functional> using namespace std; #define pi acos(-1) #define endl '\n' #define srand() srand(time(0)); #define me(x) memset(x,0,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); cin.tie(0); typedef long long LL; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; //const int dx[]={-1,0,1,0,-1,-1,1,1}; //const int dy[]={0,1,0,-1,1,-1,1,-1}; const int maxn=2e5+100; const int maxx=1e5+100; const double EPS=1e-7; const int MOD=10000007; #define mod(x) ((x)%MOD); template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} #define FOR(x,n,i) for(int i=x;i<=n;i++) #define FOr(x,n,i) for(int i=x;i<n;i++) #define W while #define sgn(x) ((x) < 0 ? -1 : (x) > 0) char str[maxn], tmp[maxn]; int get_minstring(char *s) { int len = strlen(s); int i = 0, j = 1, k = 0; while(i<len && j<len && k<len) { int t=s[(i+k)%len]-s[(j+k)%len]; if(t==0) k++; else { if(t > 0) i+=k+1; else j+=k+1; if(i==j) j++; k=0; } } return min(i,j); } int main() { int t,len; scanf("%d",&t); while(t--) { scanf("%d",&len); scanf("%s",str); int ans = get_minstring(str); printf("%d\n",ans); } return 0; }
相关文章推荐
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- zoj 2006 1729 字符串最小表示法 模板
- zoj 3204 字典序最小的最小生成树的输出
- ZOJ - 1729 Hidden Password (求串的最小表示)
- zoj 2006 1729 字符串最小表示法 模板
- ZOJ 3204 Connect them(最小生成树之Krusal 输出字典序最小的)
- Hidden Password ZOJ - 1729(最小表示法)模板题
- 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数.输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,
- Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。 Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。 Output 对于每组