2 条题解

  • 1
    @ 2024-1-7 13:57:53

    枚举前缀哈希O1判断回文

    int t;
        t =1;
        f[0]=1;
        for(int i=1;i<N;i++){
            f[i] = f[i-1]*ps;
        }
        while(t--){
            string s;
            cin >> s;
            if(s.size()==1){
                cout << s << endl;
                return 0;
            }
            ull p = 0,q = 0;
            int now = 0;
            for(int i=0;i<s.size();i++){
                p = p*ps + (s[i]-'a');
                q = 1ull*(s[i]-'a')*f[i]+q;
                if(p==q){
                    now = max(i,now);
                }
            }
            for(int i=s.size()-1;i>now;i--){
                cout << s[i];
            }
            cout << s << endl;
        }
        return 0;
    
    • 0
      @ 2024-1-7 13:30:36

      从右边枚举找到最短的回文串

      import java.io.*;
      
      public class Main {
          static BufferedReader bw = new BufferedReader(new InputStreamReader(System.in));
          static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
          public static void main(String[] args) throws Exception{
              String str = bw.readLine();
             if(str.length() == 1) System.out.println(str);
             else {
                 for (int i = str.length() - 1; i >= 0; i--) {
                     if(aaa(str, i)){
                         pw.print(new StringBuffer(str.substring(i + 1)).reverse());
                         break;
                     }
                 }
                 pw.println(str);
                 pw.flush();
             }
      
          }
          public static boolean aaa(String str, int i){
              for (int j = 0; j <= i / 2; j++) {
                  if(str.charAt(j) != str.charAt(i - j)) return false;
              }
              return true;
          }
      }
      
      • 1

      信息

      ID
      591
      时间
      1000ms
      内存
      256MiB
      难度
      9
      标签
      (无)
      递交数
      172
      已通过
      16
      上传者