[面试编程题]算法基础-字符移位
错误代码
#include<iostream> #include<string> using namespace std; bool isLow(char c) { if( (c>='A') && (c<='Z')) return false; else return true; } bool isCap(char c) { return !isLow(c); } int main() { string s; while(cin>>s) { int n=s.size(); int l=0,r=n-1; while(l<r) { while(r>=0 && isCap(s[r])) r--;//r最右边的一个小写字母 while(l<=n-1 && isLow(s[l])) l++;//l指向最左边的大写字母 if(l<r) swap(s[l],s[r]); else break; } cout<<s<<endl; } }
测试用例
测试用例:
hxKLAGLLzPyTxsFsrUnnSKQBHdQQrOyaEYJRgiJbHIDXFcQkFmIhPNKIBfHxXDBdKAvgZiBLVwnlxJAHmttsSJkZhSmQneNVoKoIYZRjPqsrFFaaqZbyNyeRjVKVFrCGdfycidTqbyQcpAtdRGzzBAaKoqybWMOyhrCQdwcRwQQpQavTnAbjriVwxJOrTYJVGYSWzKYeNAGqBzkJLucabNYvyVFxAGKLfqHXNttaqZfncEdTroGMzZnDbvZBBaRbJvuYIvlWrKaaGrvtyxrsCUOqxdwCrmVEeDrLKZKFJVRmrLsmbmOGUJyfdZIrFhuSwJQGRTYMLxKQNMaCavatlQIRZmFQvyWgQTVENxUcPKQCaUQbjyfaNuwoNdTBNldgrtPUcQodqsuJOdDpUczJWCZaasDdEYJkvituMHrCmZQSlRjIefVisatIUtfxBeKnHPyvWUKzRliFsYWgeXogiEgXDbfxAybwFuqFyEvjfIHEPDPKqEiGUtZhdDIDBGKpvBFyqHeEEhAToAbqHEpIdIhIGBtWjGHiQRctZxQQYkfFoWUbqZyIcjRPQBilHrnqNBzFmoRUYCSrGkawJCcOrMceegISpIpSGVjbngWVMTYtGoAlQFPFyOFAxndJZNfKDTwFIxisKTjyjchidXpYgLfoBOLriuIAHmAbQwoHBgbdUYBHlDQGZJASsHszOEPthLVnYbNqWegmONexfdsTVYHgtDmlyugefOBsqmgNDBoxkkhVHfvrYooVOyxDJQJLjYSngksbTopoPJFsKQzHePLukXyYTYCeW
对应输出应该为:
hxzyxssrnndryagibckmhfxdvgiwnlxmttskhmneoojqsraaqbyyejrdfycidqbycptdzzaoqybyhrdwcwpavnbjriwxrzeqzkucabvyxfqttaqfncdroznbvabvuvlraarvtyxrsqxdwrmermrsmbmyfdrhuwxaavatlmvygxcabjyfauwodldgrtcodqsudpczaasdkviturmljefisattfxenyvzlisgeogigbfxybwuqyvjfqithdpvyqehobqpdhtjictxkfobqycjilrnqzmorkawcrceegppjbngtolyxndfwxisjyjchidpgforiumbwogbdlsszthnbqegmexfdsgtmlyugefsqmgoxkkhfvrooyxjngksboposzeukyeKLAGLLPTFUSKQBHQQOEYJRJHIDXFQFIPNKIBHXDBKAZBLVJAHSJZSQNVKIYZRPFFZNRVKVFCGTQARGBAKWMOCQRQQQTAVJOTYJVGYSWKYNAGBJLNYVFAGKLHXNZETGMZDZBBRJYIWKGCUOCVEDLKZKFJVRLOGUJZIFSJQGRTYMLKQNMCQIRZFQWQTVENUPKQCUQNNTBNPUQJODUJWCZDEYJMHCZQSRIVIUBKHPWUKRFYWXEXDAFFEIHEPDPKEGUZDIDBGKBFHEEATAHEIIIGBWGHQRZQQYFWUZIRPQBHNBFRUYCSGJCOMISISGVWVMTYGAQFPFOFAJZNKDTFIKTXYLBOLIAHAQHBUYBHDQGZJASHOEPLVYNWONTVYHDOBNDBVHYVODJQJLYSTPJFKQHPLXYTYCW
你的输出为:
hxeykuezzsyoxspsronnbskgndjxryyaoorvgifbhkkxocgkmmqhsfegufyxlmtdgsvgdifxewnlxmgemttsqbknhhmtnezsosoldbgjoqsrwbaaqmbyuyeijrofgrpddfycidiqbyhcpctdjyzzjsaioqybxwfyhrdndwcxwylpoavtngbjrinwxbjrppgeecrczwaekroqmzkzqucabnrvylixjcyqfqbofttaqkfncxdtrocizjntbvhdpaqbbvuohvlerqaayrvtyxrsvpdqxdwhrmtieqrfjvyquwbmrysmbmxfbgyfdigrohuewgsilzvynxexftatavatlasifmejvylgmrutixvckdsaaazbjyfacuwopddusldgrtqdcoQUPNBJOTDNUNQJWCZUCQDKEYJPUNEVMHTCQZQSWRQIFZVRIQCIUMNQBKKLHPMYWUKTRRGFQYWJSXFIZEJXDUGOALRVFJFFKEZKLIHEPDPKDEEGUVZCODIDBGKUCBFGKHWEEIATYAJRHEBIBIZIGBDWZGHMQRGTZEQQYZNFXWUHLZKIGARPQBFVHYNLNBJFBGRUYCSAGNYKJCWOSMYGVJISYITSGVOJVAWVMTYTGQAQQFPFQOFARQCJZNOKDTMFIWKAKTBGRAQTGXCYFLVKBOLVRNIAHZAFQFPHBRZYUYBHIDQGZJASKHVNOEPQSLVZYJNSWHAJONVLBZATVYHKBDDXHBIKNOBPIFQNDBFXDIHVHJRJYYEVOOQDJQJLQYSHBQKSTUFTPJFPKQLHLPLGAXLYTYCKW