zoukankan      html  css  js  c++  java
  • Codeforces Round 56-B. Letters Rearranging(思维)

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given a string ss consisting only of lowercase Latin letters.

    You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it.

    Let's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings "abacaba", "aa" and "z" are palindromes and strings "bba", "xd" are not.

    You have to answer tt independent queries.

    Input

    The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — number of queries.

    Each of the next tt lines contains one string. The ii-th line contains a string sisi consisting only of lowercase Latin letter. It is guaranteed that the length of sisi is from 11 to 10001000 (inclusive).

    Output

    Print tt lines. In the ii-th line print the answer to the ii-th query: -1 if it is impossible to obtain a good string by rearranging the letters of sisiand any good string which can be obtained from the given one (by rearranging the letters) otherwise.

    Example

    input

    Copy

    3
    aa
    abacaba
    xdd
    

    output

    Copy

    -1
    abaacba
    xdd

    Note

    In the first query we cannot rearrange letters to obtain a good string.

    Other examples (not all) of correct answers to the second query: "ababaca", "abcabaa", "baacaba".

    In the third query we can do nothing to obtain a good string.

    题解:排个序看是否是回文串即可

    代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    
    int main()
    {
    	int n;
    	cin>>n;
    	char s1[1005];
    	for(int t=0;t<n;t++)
    	{
    		scanf("%s",s1);
    		sort(s1,s1+strlen(s1));
    		int cnt=0;
    		for(int j=1;j<strlen(s1);j++)
    		{
    			if(s1[j]==s1[j-1])
    			{
    				cnt++;
    			}
    		}
    		if(cnt==strlen(s1)-1)
    		{
    			printf("-1
    ");
    		}
    		else
    		{
    			puts(s1);
    		}
    	}
    	
    
    	return 0;
    } 
  • 相关阅读:
    美国常青小组:“4+2”,成功的企业的普遍特征
    与你的商业伙伴建立信任关系的12条准则
    成功12级跳:你还是穷人只因为你没有立下成为富人的目标
    生日与谁共
    猎取人心的36条黄金法则
    谢谢你能为我落泪
    要锤炼出营销魔法,口碑营销“无招胜有招”
    怎样成为下一个比尔·盖茨?总结决定他成功的几大要素
    只要你能够幸福
    史玉柱:创业不是靠忽悠,我的最后四个忠告
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781920.html
Copyright © 2011-2022 走看看