openjudge 1.7 字符串 34 回文字符串
http://noi.openjudge.cn/ch0107/34/
#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>'
想知道为什么会出现这个错误,应该怎么正确传字符串引用。时间超限问题怎么解决。
用字符数组更简单
更改时间限制