codefoces 620B

题意:给定n个长度为m的字符串 可以进行删除0个至多个的字符串 使得最后保留的字符串 是一个回文字符串 (ps:空字符串也是回文串)

思路:如果这些字符串中有回文串 那么一定要放在中间 只需要记录下来那个长度最大的即可
然后把字符串中放入一个集合当中
遍历所有的字符串 如果当中有的字符串等于它的倒置 那么就可以记录下来这个两个字符串 然后一定要从集合中 把这个两个删去
最后把所有的字符串按条件输出即可

字符串倒置函数:在这里插入图片描述#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)// 检查是否一个字符串是否为回文子串
{
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());// 字符串倒置函数 但是会改变同事会直接改变这个字符串而不是有一个返回值是倒置的
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;
}`

猜你喜欢

转载自blog.csdn.net/weixin_45672411/article/details/104407744
今日推荐