。重新排列字符串的字符,使其原始位置不显示任何字符。如果可以重新排列,则打印重新排列的字符串,否则打印“不可能”。 [英] . Rearrange the characters of the string such that no character appears in its original position. If the rearrangement is possible, print the rearranged string else print 'not possible'.

查看:133
本文介绍了。重新排列字符串的字符,使其原始位置不显示任何字符。如果可以重新排列,则打印重新排列的字符串,否则打印“不可能”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将获得一个字符串作为输入。重新排列字符串的字符,使其原始位置不会出现任何字符。

如果可以重新排列,请打印重新排列的字符串,否则打印不可能。



样本输入:

案例1:

aabccbadd



案例2:< br $> b $ b abaab



样本输出:

案例1:

dcaabcdab(它是'aabccbadd'可能有效的重新排列之一)



案例2:

不可能



我尝试了什么:



i尝试重新排列,但面对总和难度,应使用什么逻辑。

You are given a string as an input. Rearrange the characters of the string such that no character appears in its original position.
If the rearrangement is possible, print the rearranged string else print 'Not possible'.

Sample Input:
Case 1:
aabccbadd

Case 2:
abaab

Sample Output:
Case 1:
dcaabcdab(It is one of the possible valid rearrangements of 'aabccbadd')

Case 2:
Not possible

What I have tried:

i tried to rearrange ,but facing sum difficulty ,what logic should be used.

推荐答案

Google是你的朋友:好好经常拜访他。他可以比在这里发布问题更快地回答问题...



快速搜索使用主题的开头作为搜索词给出了50万次点击:重新排列字符串的字符,使其原始位置不显示任何字符 - Google搜索 [ ^ ]

所以你的作业已被搜索 - 并完成 - 之前很多次。

顶部链接为您提供算法和完整代码!

重新排列一个字符串,以便所有相同的字符变得距离很远 - GeeksforGeeks [< a href =http://www.geeksforgeeks.org/rearrange-a-string-so-that-all-same-characters-become-at-least-d-distance-away/target =_ blanktitle = 新窗口> ^ ]

但是......请记住,您的导师也可以访问Google,并且会识别被盗代码......:笑:



将来,请尝试自己做至少基础研究,不要浪费你的时间或我们的时间。
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using the beginning of your subject as the search term gave half a million hits: Rearrange the characters of the string such that no character appears in its original position - Google Search[^]
So your homework has been searched for - and done - many times before.
The top link gives you the algorithm, and full code!
Rearrange a string so that all same characters become d distance away - GeeksforGeeks[^]
But...do bear in mind that your tutor has access to Google as well, and will recognise stolen code... :laugh:

In future, please try to do at least basic research yourself, and not waste your time or ours.


这篇关于。重新排列字符串的字符,使其原始位置不显示任何字符。如果可以重新排列,则打印重新排列的字符串,否则打印“不可能”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆