ZJUT OJ 1031

对称串 
Time Limit:1000MS  Memory Limit:32768K

Description:

有一些字串,有些对称,有些不对称,请将对称的串按长度排列。

 

Sample Input:

123321

123454321

123

321

sdfsdfd

121212

\\dd\\

Sample Output:

123321

\\dd\\

123454321

code :
 1 /*

 2 

 3 */

 4 #include <iostream>

 5 #include <string>

 6 #include <vector>

 7 #include <algorithm>

 8 using namespace std;

 9 

10 struct cmp

11 {

12     public:

13         bool operator () (const string &s1, const string &s2) {

14             return s1.length() < s2.length();

15         }

16 };

17 bool is_symmetrical (string str);

18 

19 int main()

20 {

21     vector<string> ve;

22     for (string str; cin >> str;) {

23         if (is_symmetrical(str))

24             ve.push_back(str);

25     }

26     if (ve.size()) {

27         sort(ve.begin(), ve.end(), cmp());

28         int i = 0, len = ve.size();

29         for (; i < len; i++)

30             cout << ve[i] << endl;

31     }

32 }

33 

34 bool is_symmetrical (string str) {

35     bool symmetry = true;

36     int len  = str.length(), i = 0;

37     for (; i < len / 2; i++) {

38         if (str[i] != str[len - i - 1])

39             symmetry = false;

40     }

41     return symmetry;

42 }

 

你可能感兴趣的:(OJ)