判断是否为n回文,比如 a b a 是1 回文, abcdab是2回文。
输入: abcabc|3 这种格式,输出true or false
#include <iostream> #include <string> #include <sstream> using namespace std; bool judge_n_pal(string str,int n) { if(str.empty() || n <= 0 || str.size()%n != 0) return false; int i = 0; int j = str.size()-n; while(i<j) { for(int p = 0; p < n;p++) { if(str[i+p] != str[j+p]) return false; } i = i + n; j = j - n; } return true; } int main() { string input; cin>>input; string content; int x = input.find_first_of('|'); content = input.substr(0,x); int n; string str_n = input.substr(x+1,input.length()-x-1); stringstream(str_n)>>n; bool ret = judge_n_pal(content,n); cout<<input<<" "<<ret; }