(1)暴力 这里不会超时
举出所有可能的起始和终止
这里注意一下用到了c++中的getline函数
原型如下
1 istream& getline (istream& is, string& str, char delim); 2 3 istream& getline (istream& is, string& str);
第一个参数是istream的引用第二个参数是子串引用
这里用c中的fgets也可以实现读一行的效果
#include <cstdio> #include <string> #include <algorithm> #include <iostream> using namespace std; string st; bool issy(int i,int j) { int flag=1; while(i <= j) { if(st[i] != st[j]) flag=0; i++; j--; } return flag == 0? false:true; } int main() { getline(cin,st); int maxlen=0; for(int i=0;i<st.size();i++) { for(int j=i;j<st.size();j++) { if(issy(i,j)) { maxlen=max(maxlen,j-i+1); } } } printf("%d",maxlen); return 0; }