UVA1584 Circular Sequence【字符串】

版权声明:版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/YT201758501112/article/details/83713589

Circular Sequence

 UVA - 1584 

题目传送门

题目大意:输入一个环形字符串,需输出其最小字典序的形式的字符串。

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
char str1[220];
char ans[120];
char nape[120];
int main() 
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
    	scanf("%s",str1);
    	char c='a';
    	int len=strlen(str1);
    	strcpy(ans,str1);
    	int i;
    	for(i=0;i<len;i++)
    	{
    		c=min(c,str1[i]);     //先求出其最小的字母
    	}
    	/*printf("%c\n",c);*/
    	for(i=len;i<2*len-1;i++)
    		str1[i]=str1[i-len];     //将原字符串扩展为环状形式
    	str1[i]='\0';
    	/*printf("%s\n",str1);*/
    	for(i=0;i<len;i++)
    	{
    		ms(nape);
    		if(str1[i]==c)
    		{
    			int j,k;
    			for(j=i,k=0;j<i+len;j++,k++)
    				nape[k]=str1[j];
    			nape[k]='\0';
    			if(strcmp(ans,nape)>0)        //求出字典序最小的字符串
    				strcpy(ans,nape);
    		}
    	}
    	printf("%s\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/YT201758501112/article/details/83713589