zoukankan      html  css  js  c++  java
  • 5. 最长回文子串

    #include<iostream>
    #include<stdio.h>
    #include<cstdio>
    #include<vector>
    #include <algorithm>
    #include <functional>
    #include<string>
    #include<fstream>
    #include <sstream>
    #include <assert.h>
    using namespace std;
    class Solution {
    public:
    	string longestPalindrome(string s)
    	{
    		if (s.length() < 0) return "";//如果长度小于0则返回空
    		int n = s.size();//获得字符串的长度
    		vector<vector<bool>> dp(n, vector<bool>(n, false));//定义bool向量
    		int maxlen = 1, start = 0;
    		for (int i = 0; i < n; i++)
    		{
    			for (int j = 0; j <= i; j++)
    			{
    				if (i - j < 2) dp[i][j] = (s[i] == s[j]);
    				else
    					dp[i][j] = (s[i] == s[j] && dp[i - 1][j + 1] == 1);
    				if (dp[i][j] && maxlen < i - j + 1)
    				{
    					maxlen = i - j + 1;
    					start = j;
    				}
    			}
    		}
    		return s.substr(start, maxlen);
    	}
    
    };
    
    string stringToString(string input) {
    	assert(input.length() >= 2);
    	string result;
    	for (int i = 1; i < input.length() - 1; i++) {
    		char currentChar = input[i];
    		if (input[i] == '\') {
    			char nextChar = input[i + 1];
    			switch (nextChar) {
    			case '"': result.push_back('"'); break;
    			case '/': result.push_back('/'); break;
    			case '\': result.push_back('\'); break;
    			case 'b': result.push_back(''); break;
    			case 'f': result.push_back('f'); break;
    			case 'r': result.push_back('
    '); break;
    			case 'n': result.push_back('
    '); break;
    			case 't': result.push_back('	'); break;
    			default: break;
    			}
    			i++;
    		}
    		else {
    			result.push_back(currentChar);
    		}
    	}
    	return result;
    }
    
    int main() {
    	string line;
    	while (getline(cin, line)) {
    		string s = stringToString(line);
    
    		string ret = Solution().longestPalindrome(s);
    
    		string out = (ret);
    		cout << out << endl;
    	}
    	return 0;
    }
    

      

    # 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
    # 示例 1:
    # 输入: "babad"
    # 输出: "bab"
    # 注意: "aba" 也是一个有效答案。
    # 示例 2:
    # 输入: "cbbd"
    # 输出: "bb"
    # 解题思路:
    # 正着与倒着比.
    import sys
    class Solution:
        def zuichanghuiwen(self, s):
            if len(s) <= 1:
                return s
            for length in range(len(s), 0, -1):
                for i in range(0, len(s) - length + 1):
                    now_s = s[i:i + length]
                    if now_s == now_s[::-1]:
                        return now_s
    if __name__ == '__main__':
        s = sys.stdin.readline().strip()
        ret = Solution().zuichanghuiwen(s)
        print(ret)
    

      

  • 相关阅读:
    window 编译lua 5.3
    邮件服务器软件
    mkyaffs2image 生成不了120M的镜像文件的解决方法
    C static struct
    uboot 如何向内核传递参数
    linux 链接理解
    snmp 协议之理解
    交叉编译知识点总结
    回滚原理 Since database connections are thread-local, this is thread-safe.
    REST 架构的替代方案 为什么说GraphQL是API的未来?
  • 原文地址:https://www.cnblogs.com/277223178dudu/p/10763177.html
Copyright © 2011-2022 走看看