Advertisement
MaskerQwQ

判断回文串(结尾+@)

Nov 15th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<string.h>
  3. #include<stack>
  4.  
  5. using namespace std;
  6.  
  7. void Palindrome(string l){
  8.     stack<char>s;
  9.     int flag=0;
  10.     if(l.length()%2==0){
  11.         int mid=l.length()/2;
  12.         for(int i=0;i<mid-1;i++){
  13.             s.push(l[i]);
  14.         }
  15.         for(int i=mid;i<l.length()-1;i++){
  16.             if(s.top()!=l[i]){
  17.                 flag=1;
  18.                 break;
  19.             }
  20.             s.pop();
  21.         }
  22.     }else{
  23.         int mid=l.length()/2;
  24.         for(int i=0;i<mid;i++){
  25.             s.push(l[i]);
  26.         }
  27.         for(int i=mid;i<l.length()-1;i++){
  28.             if(s.top()!=l[i]){
  29.                 flag=1;
  30.                 break;
  31.             }
  32.             s.pop();
  33.         }
  34.     }
  35.     if(flag==0){
  36.         cout<<"此字符串是回文串"<<endl;
  37.     }else{
  38.         cout<<"此字符串不是回文串"<<endl;
  39.     }
  40. }
  41. int main(){
  42.     string t;
  43.     cin>>t;
  44.     Palindrome(t);
  45.     return 0;  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement