openjudge 字符串 c++

openjudge 1.7 字符串 34 回文字符串
http://noi.openjudge.cn/ch0107/34/

img


以下为代码


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int ifpa(string r);
int main()
{
    string s,y;
    cin>>s;
    int i=0,j=0,count=0;
    for(;j<s.length();)
    {
        j=++count;
        for(i=0;j<s.length();++i,++j)
        {
          if(ifpa(s.substr(i,j-i+1)))//从左向右取字符串判断
          {
              y=s.substr(i,j-i+1);
            cout<<y<<endl;
          }
        }
        j=0;
    }
    return 0;
}
int ifpa(string r)//判断是否为回文
{
    string S;
    S=r;
    reverse(r.begin(),r.end());
    if(r==S)
    {
        return 1;
    }else
    {     
    return 0;
    }
}

问题:输出测试数据正确,但时间超限了。
修改:尝试将传字符串改成传引用 节省时间,但出现错误

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int ifpa(string &r);
int main()
{
    string s,y;
    cin>>s;
    int i=0,j=0,count=0;
    for(;j<s.length();)
    {
        j=++count;
        for(i=0;j<s.length();++i,++j)
        {
          if(ifpa(s.substr(i,j-i+1)))//从左向右取字符串判断
          {
              y=s.substr(i,j-i+1);
            cout<<y<<endl;
          }
        }
        j=0;
    }
    return 0;
}
int ifpa(string &r)//判断是否为回文
{
    string S;
    S=r;
    reverse(r.begin(),r.end());
    if(r==S)
    {
        return 1;
    }else
    {     
    reverse(r.begin(),r.end());
    return 0;
    }
}

[Error] cannot bind non-const lvalue reference of type 'std::string&' {aka 'std::__cxx11::basic_string<char>&'} to an rvalue of type 'std::__cxx11::basic_string<char>'

我也看到了这篇文章,但没有看懂。

C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘myString_发狂的蜗牛的博客-CSDN博客 先看代码(不想看代码可以直接看代码后的问题描述)1234567891011121314151617181920212223242526... https://blog.csdn.net/digitalkee/article/details/105092400?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163998505916780274115810%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163998505916780274115810&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-105092400.pc_search_result_cache&utm_term=%5BError%5D+cannot+bind+non-const+lvalue+reference+of+type+%27std%3A%3AString%26%27+to+an+rvalue+of+type+%27std%3A%3AStr&spm=1018.2226.3001.4187

想知道为什么会出现这个错误,应该怎么正确传字符串引用。时间超限问题怎么解决。

用字符数组更简单

更改时间限制