zoukankan      html  css  js  c++  java
  • UVA

    //环状序列 uva1584
    //收获:
    //如何判断环状序列的字典序:取余的巧妙应用,但是注意仅比较长度次数,否则循环可能无法退出,故要 n = strlen(s) i从0到(n-1), 比较n次即可 
    
    
    #include <iostream>
    #include <cstring>
    using namespace std;
    int maxn = 105;
    //环状串s的表示法p是否比表示法q的字典序小 
    int CompareSequence(const char*s, int p, int q)
    {
    	int n = strlen(s);
    	for (int i = 0; i < n; i++)
    		if (s[(i + p) % n] != s[(i + q) % n])
    			return ( s[(i + p) % n] < s[(i + q) % n] );
    			
    	return 0;
    }
    int main()
    {
    	int t;
    	cin >> t;
    	char s[105];
    	while (t--)
    	{
    		cin >> s;
    		int ans = 0, n = strlen(s);
    		for (int i = 0; i < n; i++)
    		if (CompareSequence(s, i, ans)) ans = i;
    		
    		for (int i = 0; i < n; i++)
    		cout << s[(i + ans) % n];
    		cout << endl;
    	}
    	return 0;
    }

  • 相关阅读:
    团队冲刺0202
    团队冲刺0201
    第十五周
    第十四周博客
    十三周总结
    软件设计模式13
    软件设计模式12
    软件构造4
    软件设计模式11
    软件设计模式10
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789454.html
Copyright © 2011-2022 走看看