Jiudu _ Question 1369: String arrangement, jiudu 1369
// Find the full arrangement of a string. I feel it is too difficult to implement it myself. Deterministic things are easy to find, but a little uncertain things are hard to complete. The algorithm next_permutation (arr, arr + strlen (arr) in the standard template library is really good. Although the use of cin cout times out, it can be changed to scanf printf.
Description:
Enter a string and print all characters in the string in lexicographically. For example, if abc is input, all the strings abc, acb, bac, bca, cab, and CBA that can be arranged by characters a, B, and c are printed.
Input:
Each test case contains one line.
Enter a string of no more than 9 characters (repeated characters may exist), including only uppercase and lowercase letters.
Output:
All data groups are output in Lexicographic Order.
Sample input:
Abc
BCA
Sample output:
Abc
Acb
Bac
Bca
Cab
CBA
ABC
ACB
BAC
BCA
CAB
CBA
#include<algorithm>#include <stdio.h>#include <string.h>using namespace std;int main(){ char *arr=new char[10]; while(scanf("%s",arr)!=EOF) { sort(arr,arr+strlen(arr)); printf("%s\n",arr); while(next_permutation(arr,arr+strlen(arr))) printf("%s\n",arr); } return 0;} /************************************************************** Problem: 1369 User: hndxztf Language: C++ Result: Accepted Time:130 ms Memory:1024 kb****************************************************************/
String sorting
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char stra [10] [21];
Char t [21];
Int n, I, j;
Scanf ("% d", & n );
Getchar ();
For (I = 0; I <n; I ++)
{
Gets (stra [I]);
}
For (I = 0; I <n-1; I ++)
{
For (j = I + 1; j <n; j ++)
{
If (strcmp (stra [I], stra [j])> 0)
{
Strcpy (t, stra [I]);
Strcpy (stra [I], stra [j]);
Strcpy (stra [j], t );
}
}
}
For (I = 0; I <n; I ++)
Printf ("% s \ n", stra [I]);
Return 0;
}
String sorting problems
Let me give you a function that I wrote myself. Haha, it uses ASC sorting.
The call method is Response. write (strSort (array ("hi", "hello", "good", "well"), true ))
There are instructions. Remember to use array input!
'Sort the arrays by Bubble sorting, which is applicable to sorting in Chinese and English
'Asc character sorting rule, that is, to calculate the ASC code of the first character in a string. You can use Chr to return the reverse ASC code. But the element length in the array cannot be only 1 !!!
'Arraylist Array
'Isasc is in ascending order
'Return value Array
Public Function strSort (ByVal ArrayList, ByVal isAsc)
Dim intI, intJ, NewArray, CacheArray, CacheTemp, ArrTemp, intCount
ReDim CacheArray (Ubound (ArrayList ))
NewArray = ArrayList
IntCount = Ubound (NewArray)
For intI = 0 To intCount
CacheArray (intI) = Asc (NewArray (intI ))
Next
If IsAsc Then
For intI = 0 To intCount
For intJ = intCount-1 To intI Step-1
If CacheArray (intJ)> CacheArray (intJ + 1) Then
CacheTemp = CacheArray (intJ)
CacheArray (intJ) = CacheArray (intJ + 1)
CacheArray (intJ + 1) = CacheTemp
ArrTemp = NewArray (intJ)
NewArray (intJ) = NewArray (intJ + 1)
NewArray (intJ + 1) = ArrTemp
End If
Next
Next
Else
For intI = 0 To intCount
For intJ = intCount-1 To intI Step-1
If CacheArray (intJ) <CacheArray (intJ + 1) Then
CacheTemp = CacheArray (intJ)
CacheArray (intJ) = CacheArray (intJ + 1)
CacheArray (intJ + 1) = CacheTemp
ArrTemp = NewArray (intJ)
NewArray (intJ) = NewArray (intJ + 1)
NewArray (intJ + 1) = ArrTemp
End If
Next
Next
End if
StrSort = NewArray
End Function... remaining full text>