Leetcode Practice-String

Source: Internet
Author: User


First, palindrome-partitioning

Topic description
Given a string s, partition s such that every substring to the partition is a palindrome.
Return all possible palindrome partitioning of S.
For example, given s = "AaB",
Return
[
["AA", "B"],
["A", "a", "B"]
]

Train of thought: Dynamic programming + depth First traversal (DFS)

1, using the dynamic specification algorithm to divide the sub-palindrome string

2. Using DFS to traverse possible situations

Code:

Class Solution {public:vector<vector<string> > partition (String s) {VECTOR&LT;VECTOR&LT;STRING&G T
        > result;
        Vector<string> Array;
        if (S.length () ==0) return result;

        vector<vector<bool> > Flag = DP (s);
        DFS (s, 0, flag, array, result);
    return result; } void Dfs (string s, int begin, Vector<vector<bool> > Flag, vector<string> Array, vector<vector& 
            lt;string> > &result) {if (Begin==s.length ()) {result.push_back (array);
            Array.clear ();  
        Return for (int i=begin;i<s.length (); ++i) {if (flag[begin][i]==1) {Vector<stri
                Ng> Temp (array);
                Temp.push_back (S.substr (Begin,i+1-begin)); cout << "begin=" << begin << ";
                I+1= "<< (i+1) << Endl; cout << array.back () << EndL  
            DFS (s, i+1, flag, temp, result);
        }} vector<vector<bool> > dp (string s) {int len = s.length ();
        vector<vector<bool> > Flag (len, vector<bool> (len, 0));
        int I, J;  for (i=len-1; i>=0;-i) {for (j=i; j<len; ++j) {if (j = = i) flag[i][j]
                = true; else{if (s[i] = = S[j] && (j = = I+1 | | flag[i+1][j-1] = = True)) {Flag[i
                    ][J] = true;
    }}} return flag; }

};

Local Test code:

Vector<vector<bool> > DP (string s);//dynamic programming, molecular string
void Dfs (string s, int begin, Vector<vector<bool > > Flag, vector<string> array, vector<vector<string> > &result);//Depth First traversal
vector< Vector<string> > partition (string s);
int main () {
	string s = ' Abcbad ';
	cout << s.substr (1, 2) << Endl;

	vector<vector<bool> > Flag = DP (s);
	int I, J;
	For (i=0 i<flag.size (); ++i) {//Print flag for
		(j=0; j<flag[i].size (); ++j)
			cout << Flag[i][j] << ' ';
		cout << Endl;
	}

	vector<vector<string> > result;
	vector<string> Array;
	DFS (s, 0, flag, array, result);
	vector<vector<string> > result = partition (s);
	
	For (i=0 i<result.size (), ++i) {for
		(j=0; j<result[i].size (); ++j) {
			cout << Result[i][j] < < ';
		}
		cout << Endl;
	}
	return 0;
}

Second, Add-binary

Topic description
Given two binary strings, return their sum (also a binary string).
For example,
A = "11"
b = "1"
Return "100"


Code:

Class Solution {public:string addbinary (string A, string b) {string sum;//output int up=0;//rounding
        int I=a.length ()-1;
        int J=b.length ()-1;
                while (i>=0 && j>=0) {int temp = a[i]+b[j]-' 0 '-' 0 ' + up;//the number on the current bit and if (temp = = 3) {
                up = 1;
            Sum.insert (Sum.begin (), ' 1 ');
                else if (temp = = 2) {up = 1;
            Sum.insert (Sum.begin (), ' 0 ');
                else if (temp = = 1) {up = 0;
            Sum.insert (Sum.begin (), ' 1 ');
                else if (temp = = 0) {up = 0;
            Sum.insert (Sum.begin (), ' 0 ');
            } I.
        --j; 
            while (i>=0) {//At this point if i>=0, description a long, operation of the remaining digits of a int temp = a[i]-' 0 ' +up;
                if (temp = = 2) {up = 1;
            Sum.insert (Sum.begin (), ' 0 '); else if (temp = 1) {up = 0;
            Sum.insert (Sum.begin (), ' 1 ');
                else if (temp = = 0) {up = 0;
            Sum.insert (Sum.begin (), ' 0 ');
        } I.
            while (j>=0) {//At this point if j>=0, description b length, the remaining number of digits to b operation int temp = b[j]-' 0 ' + up;
                if (temp = = 2) {up = 1;
            Sum.insert (Sum.begin (), ' 0 ');
                else if (temp = = 1) {up = 0;
            Sum.insert (Sum.begin (), ' 1 ');
                else if (temp = = 0) {up = 0;
            Sum.insert (Sum.begin (), ' 0 ');
        }--j;
        The IF (up = = 1)//Determines whether the highest bit has a carry Sum.insert (Sum.begin (), ' 1 ');
    return sum; }
};

Local Test code:

int main () {
	string A;
	string B;
	
	cout << "A:";
	Getline (Cin, a);
	cout << "B:";
	Getline (CIN, b);
	
	string s = Addbinary (A, b);
	string s = "ss";
	cout << s << endl;
	return 0;
}








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.