本文给出五种C++程序实现的代码!前三种解答程序,已Accepted !是从网上整理来的,加上本人一些分析和代码备注,让看的朋友更容易理解。
解答思路一:(最快解答)
用哈希表来建立每个单词和其位置的映射,一个set来保存出现过的单词的长度。
算法的思想是,遍历单词集,对于遍历到的单词,我们对其翻转一下,然后在哈希表查找翻转后的字符串是否存在,注意不能和原字符串的坐标位置相同,因为有可能一个单词翻转后和原单词相等,现在我们只是处理了bat和tab的情况,还存在abcd和cba,dcb和abcd这些情况需要考虑,这就是我们为啥需要用set,由于set是自动排序的,我们可以找到当前单词长度在set中的iterator,然后从开头开始遍历set,遍历比当前单词小的长度,比如abcdd翻转后为ddcba,我们发现set中有长度为3的单词,然后我们dd是否为回文串,若是,再看cba是否存在于哈希表,若存在,则说明abcdd和cba是回文对,存入结果中,对于dcb和aabcd这类的情况也是同样处理,我们要在set里找的字符串要在遍历到的字符串的左边和右边分别尝试,看是否是回文对,这样遍历完单词集,就能得到所有的回文对
<span style="font-size:18px;">class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> res;
unordered_map<string, int> m;
set<int> s;
for (int i = 0; i < words.size(); ++i) {
m[words[i]] = i;
s.insert(words[i].size());
}
for (int i = 0; i < words.size(); ++i) {//遍历一遍所有的单词
string t = words[i];
int len = t.size();
reverse(t.begin(), t.end());//反转单词
if (m.count(t) && m[t] != i) {//反转单词在map中,并且不是同一个单词,加入结果,如单词表中有两单词aba 和 aba
res.push_back({i, m[t]});
}
auto a = s.find(len);
for (auto it = s.begin(); it != a; ++it) {
int d = *it;
if (isValid(t, 0, len - d - 1) && m.count(t.substr(len - d))) {//反转单词aaba,若set中存在单词ba,且前半部分aa为回文。
res.push_back({i, m[t.substr(len - d)]});
}
if (isValid(t, d, len - 1) && m.count(t.substr(0, d))) {//反转单词baaa,若set中存在单词ba,且后半部分aa为回文。
res.push_back({m[t.substr(0, d)], i});
}
}
}
return res;
}
bool isValid(string t, int left, int right) {//判断单词t[left]到t[right]是否构成回文。
while (left < right) {
if (t[left++] != t[right--]) return false;
}
return true;
}
};</span>
解答思路二:(很容易想到的算法)
考虑所有构成回文的情况
1 存在空单词;遍历所有单词,找回文单词,如{“aa”,“”}
2 将单词反转,若反转后的单词在单词表中,这两个单词构成回文!如{“abc”,“cba”}
3 将每个单词都分成左右两部分,(将aabc分两部分: a abc;aa bc;aab c;)
若左半部分是回文,则反转右半部分,查看单词表中是否存在右半部分的反转单词;如cb与aabc构成回文。
若右半部分是回文,则反转左半部分,查看单词表中是否存在左半部分的反转单词;如aabc与baa构成回文。
class Solution {
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> res;
vector<int> temp(2);
multimap<string,int> map;
for (int i = 0; i < words.size(); ++i) {
map.insert(pair<string,int>(words[i],i));
}
//若有单词为空,则找到所有本身是回文的单词
if(map.find("")!=map.end()){
int k=(map.find(""))->second;
for(int i = 0; i < words.size(); ++i){
if(isPalindrome(words[i])&&i!=k){
temp[0]=k;
temp[1]=i;
res.push_back(temp);
temp[0]=i;
temp[1]=k;
res.push_back(temp);
}
}
}
for (int i = 0; i < words.size(); ++i) {//遍历一遍所有的单词
string word = words[i];
int n = word.size();
for(int j=1;j<n;++j){
//把单词分成左右两部分left,right
string left=word.substr(0,j);
string right=word.substr(j);
if(isPalindrome(right)){//若单词右半部分是回文
reverse(left.begin(), left.end());//左半部分反转
if(map.find(left)!=map.end()&&(map.find(left)->second)!=i){
temp[0]=i;
temp[1]=map.find(left)->second;
res.push_back(temp);
}
}
if(isPalindrome(left)){//若单词左半部分是回文
reverse(right.begin(),right.end());//右半部分反转
if(map.find(right)!=map.end()&&(map.find(right)->second)!=i){
temp[0]=map.find(right)->second;
temp[1]=i;
res.push_back(temp);
}
}
}
//反转整个单词,看反转单词是否在单词表中
reverse(word.begin(), word.end());
if(map.find(word)!=map.end()&&(map.find(word)->second)!=i){
temp[0]=i;
temp[1]=map.find(word)->second;
res.push_back(temp);
}
}
return res;
}
bool isPalindrome(string s) {//判断单词s是否构成回文。
int i=0;
int j=s.size()-1;
while (i<j) {
if (s[i++] != s[j--]) return false;
}
return true;
}
};
解题思路三:(还不错的解答,思路较复杂)
用长度为两个单词数的向量expand,保存所有单词及其对应下标(用true标记),也保存所有单词的反转单词及其原单词对应的下标(用false标记)。
对expand初始化,然后进行排序。排序是为了提高检索速度,使相同单词或者相近单词在位置上保存相近,若发现完全不匹配的单词,就可以直接跳出二层循环,结束当前单词的匹配。
能构成回文的情况有三种: abc和cba,abaa与ab,aaba与ab。第一种反转后的单词与原单词完全相同,第二种情况和第三种情况,除去相同部分,剩余部分构成回文。
程序中的函数isPalin(),返回一个整形数,返回3包含了能构成回文的三种情况,即为所求!返回2代表当前单词不匹配,继续验证expand中的下一个单词;返回1代表当前反转单词与当前expand中的其他单词没有匹配的,不需要再向后匹配,即跳出二层循环!
<span style="font-size:18px;">class Solution {
public:
vector<vector<int> > palindromePairs(vector<string>& words) {
int size = words.size();
vector<vector<int> > result;//保存得到的结果
vector<pair<string, pair<int, bool> > > expand(size*2);//保存单词和对应的下标,反转每个单词后,保存反转单词及原来的下标
for(int i = 0; i < size; ++i)
expand[i] = pair<string, pair<int, bool> >(words[i], pair<int, bool>(i, true));//保存单词和对应的下标。原单词都标记为true
for(int i = 0; i < size; ++i)
{
reverse(words[i].begin(), words[i].end());//反转每个单词
expand[i+size] = pair<string, pair<int,bool> >(words[i], pair<int, bool>(i, false));//保存反转后的单词及原单词对应的下标,反转单词标记false
}
sort(expand.begin(), expand.end());//对向量进行排序,向量中存储的是pair<string,pair<int,bool>>,所以比较顺序是单词、下标、bool
for(int i = 0; i < size*2; ++i)
{
bool flag = true;
for(int j = i+1; j < size*2 && flag; ++j)
{
if(expand[i].second.first == expand[j].second.first) //下标相同,即为同一个单词,跳过
continue;
if(! (expand[i].second.second ^ expand[j].second.second) )//同为false,即两个都是反转单词,跳过
continue;
int type = isPalin(expand[i].first, expand[j].first);//判断两个单词的情况:1有字母不同;2长度不同,且长单词剩余部分不是回文;3两个单词构成回文。
switch(type)
{
case 1: flag = false; break;//情况1,其后的单词不用再比较,直接跳过(因expand中已经排好序,相同单词必定挨着。)
case 2: break;//情况2,进行下一轮比较
case 3: //情况3,两个单词可以构成回文,将单词下标加入result
int u = expand[i].second.second?expand[i].second.first:expand[j].second.first;
int v = expand[i].second.second?expand[j].second.first:expand[i].second.first;
vector<int> tmp(2); tmp[0] = u; tmp[1] = v;
result.push_back(tmp);
break;
}
}
}
return result;
}
private:
//return 1 means a and b don't have proper prefix
//return 2 means a and b do have proper prefix,
//but the trailing part is not palindrome
//return 3 means a and b do have proper prefix,
//and the trailing part is palindrome
int isPalin(string &a, string &b)
{
int size1 = a.size(), size2 = b.size();
int i = 0;
while(i < size1 && i < size2)
{
if(a[i] != b[i]) return 1;//单词中相同位置的字母不匹配 如单词 abc 和 bde
++i;
}
if(i < size1)
{
int j = size1-1;
while(i < j)
{
if(a[i] != a[j])
return 2;//多余部分不是回文 如abce 和 ab
++i;
--j;
}
}
else if(i < size2)
{
int j = size2-1;
while(i < j)
{
if(b[i] != b[j])//多余部分不是回文,如ab 和 abed
return 2;
++i;
--j;
}
}
return 3;//两个单词完全相同,或者多余部分是回文,如abc 和 abc;又如abcc 和 ab
}
};</span>
解题思路三:暴力解法:运行超时!
<span style="font-size:18px;">class Solution {
private:
bool palindrome(string s){
int n=s.size();
int i=0;
while(i<n/2){
if(s[i]!=s[n-i-1])return false;
++i;
}
return true;
}
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<int> v1(2);
vector<vector<int>> res;
int n=words.size();
int j=0;
while(j<n){
string s=words[0];
v1[0]=j;
for(int i=1;i<n;++i){
s=words[0];
s+=words[i];
if(palindrome(s)){
if(i<=j)
v1[1]=i-1;
else
v1[1]=i;
res.push_back(v1);
}
}
++j;
if(j<=n-1){
string temp=words[0];
words[0]=words[j];
words[j]=temp;
}
}
return res;
}
};</span>
解题思路四:暴力解法:运行超时!
<span style="font-size:18px;">class Solution {
private:
bool palindrome(string s1,string s2){
int i=0;
int j=s2.size()-1;
while(i<s1.size()&&j>=0){
if(s1[i]!=s2[j])
return false;
++i;
--j;
}
if(s1.size()>s2.size()){
j=s1.size()-1;
while(i<j){
if(s1[i]!=s1[j])
return false;
++i;
--j;
}
}else if(s1.size()<s2.size()){
i=0;
while(i<j){
if(s2[i]!=s2[j])
return false;
++i;
--j;
}
}
return true;
}
public:
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<int> v1(2);
vector<vector<int>> res;
int n=words.size();
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(i!=j&&palindrome(words[i],words[j])){
v1[0]=i;
v1[1]=j;
res.push_back(v1);
}
}
}
return res;
}
};</span>