codefoces 620B

Meaning: Given n strings of length m, 0 or more strings can be deleted so that the last retained string is a palindrome string (ps: empty string is also a palindrome string)

Idea: If there are palindrome strings in these strings, they must be placed in the middle, and only the one with the largest length needs to be recorded.
Then put the strings into a set and
traverse all the strings if any of the strings are equal to it. Then you can record the two strings and then you must delete these two from the set.
Finally, output all the strings according to the conditions.

字符串倒置函数:Insert picture description here#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
string s[107];
string s1[107];
string s2[107];
string hw;

bool check(string str)// Check whether a string is a palindrome substring
{ for(int i = 0,j = str.length()-1;i <= j;i ++,j --) { if(str[i] != str[j]) return false; } return true; }






int main()
{ setq; int n,m; cin>>n>>m; int len ​​= 0; int ans = 0; for(int i = 0;i <n;i ++) { cin>>s [i]; q.insert(s[i]); if(check(s[i])) { q.erase(s[i]); if(len <s[i].length()) { len = s[i].length(); hw = s[i]; } } } ans += len; int cnt = 0; for(int i = 0;i <n;i ++) { string a = s [i]; reverse(s[i].begin(),s[i].end());// String inversion function but it will change the colleague will directly change the string instead of having a return value that is inverted if(q.count(s[i])) { s1[++cnt] = a; s2[cnt] = s[i]; q.erase(a);






























q.erase(s[i]);
}
}
//cout<<cnt<<endl;
ans += (cnt * m * 2);
printf("%d\n",ans);
for(int i = 1;i <= cnt;i ++)
cout<<s1[i];
cout<<hw;
for(int i = cnt;i > 0;i --)
cout<<s2[i];
return 0;
}`

Guess you like

Origin blog.csdn.net/weixin_45672411/article/details/104407744