Programmer's Code interview Guide-string problem: C + + language implementation
The following programs run the environment: vc6++See left Teacher's book: Programmer Code Interview Guide: it name enterprise algorithm and data structure of the optimal solution, is the Java implementation, in order to brush the problem, but the position is C/s + +, the following is my use of C + + implementation of the code to introduce a slightly simpler, later to fill
/*
//Programmer Code Interview Guide-string issues1. Judge whether two strings are variants of each other
BOOL Isdeformation (String str1,string str2)
{
if (str1.length ()!=str1.length ()) return
false;
const char *CHAS1=STR1.C_STR ();
const char *CHAS2=STR2.C_STR ();
int *map = ASC for new int[256];//character between 0-255 for
(int i=0;i<str1.length (); i++)
map[chas1[i]]++;// The array value of the ASC code values for the character + + for
(int j=0;j<str2.length (); j + +)
if (map[chas2[j]]--= 0)//first judgment again-return
false;
return true;
}
int main ()
{
string str1,str2;
Getline (CIN,STR1);
Getline (CIN,STR2);
(Isdeformation (STR1,STR2) ==true)? cout<< "yes\n": cout<< "no\n";
return 0;
}
*/ /*
2. Sum of numeric substrings in a string: Given a string of numbers that all numeric strings represent
Eg:a1cd-2ef33 ==1-2+33=21
int numsum (String str)
{
const char *str1=str.c_str ();
The int num=0;//extracts the current numeric
int sum=0;//the final and
int cur=0;//current values of
bool posi=true;
for (int i=0;i<str.length (); i++)
{
cur=str1[i]-' 0 ';//Convert to number
if (cur<0 | | cur>9)/not number
{
sum+=num;//is not a digital post computation and
num=0;
if (str1[i] = = '-')
{
if (i-1>-1 && str1[i-1]== ')//If the previous one is also-take the anti-
Posi=!posi;
else
posi=false;
}
else posi=true;//+
}
else
{
num=num*10+ (posi)? Cur:-cur);
}
sum+=num;//when the last character is a number is added here, because sum is the return sum that is computed when not for the number
;
int main ()
{
string str;
Getline (CIN,STR);
Cout<<numsum (str);
*/
/*
3. Remove substring of k 0 in string
String Removekzeros (String Str,int k)
{
char *str1=new char[str.length ()];
strcpy (Str1,str.c_str ());
int start=-1,count=0;
for (int i=0;i<str.length (); i++)
{
if (str1[i]== ' 0 ')
{
count++;
Start= ((start==-1) i:start);
else
{
if (count==k) while
(Count--! =0)
str1[start++]=0;
count=0;
Start=-1
}
}
if (count==k)//If the last character is 0 while
(Count--! =0)
str1[start++]=0;
return str=str1;
}
int main ()
{
string str;
int k;
cin>>k;
Cin.ignore ()//Do not ignore the remaining carriage returns will cause getline invalid
getline (CIN,STR);//Enter two carriage returns because of VC6 bug
Cout<<removekzeros (str , 3);
return 0;
}
*/
/*
4. Judge whether the two strings are rotated by each other: thinking: Whether or not to include a in the B+b
BOOL Isrotation (string a,string b)
{
if (a.length ()!=b.length ()) return
false;
string b2=b+b;
Return Strstr (B2.c_str (), A.c_str ())!=null;
}
int main ()
{
string a,b;
Getline (cin,a);
Getline (cin,b);
Isrotation (a,b)? cout<< "yes" <<endl:cout<< "no" <<endl;
return 0;
}
*/
/*
5. Convert an integer string to an integer value: conforming to everyday forms eg "012" does not conform, 32 bits cannot overflow
To determine whether or not to conform to everyday form functions
BOOL IsValid (const char Chas[],int len) {if (Chas[0]!= '-' && (chas[0]< ' 0 ' | | | chas[0]> ' 9 '))//First not-and number does not match
return false;
if (chas[0]== '-' && (len==1 | | chas[1]== ' 0 ')) return false;
if (chas[0]== ' 0 ' && len>1) return false;
for (int i=0;i<len;i++) if (chas[i]< ' 0 ' | | | | | chas[i]> ' 9 ') return false;
return true;
}//conversion function int convert (string str) {const char* chas=str.c_str ();
if (IsValid (Chas,str.length ())!=true)//does not conform to return 0; BOOL posi= chas[0]== '-'?
False:true; int minq=-(2147483648/10);//The following calculation if less than this number is to overflow the int minr=-(2147483648%10);//If equal to the number and the remainder is less than the number of times is overflow, because all are negative so is less than int res=
0;
int cur=0; for (int i=0;i<str.length (); i++) {cur= ' 0 '-chas[i];//negative number if (Res<minq | |
(Res==minq && Cur<minr))
return 0;
Res=res*10+cur;
} if (posi && res==-2147483648)//integer maximum is 2147483647 return 0; Return posi?
-res:res;
int main () {string str;
Getline (CIN,STR);
Cout<<convert (str);
return 0; }
*/
/*
6. Replace the string, replace the from Str with to EG:STR=123ABC FROM=ABC to=4567 return 1234567 str=123abcabc from=abc to=x return 123x: only one x attention
Idea: Replace the from character in the original string with 0, then hit 0 where Str is added
String Replace (string str,string from,string to)
{
char *str1=new char[str.length () +1];
strcpy (Str1,str.c_str ());
const char *FROM1=FROM.C_STR ();
int match=0;
for (int i=0;i<str.length (); i++)//lookup from after add 0
{
if (str1[i]==from1[match++])
{
if (match== From.length ())//IS from while
(match)
{
str1[i-match+1]=0;;
match--
}
}
else
{
match=0;
}
}
String res= "";
String Cur= "";
for (int j=0;j<str.length (), j + +)/in 0 to the place to
{
if (str1[j]!=0)
cur+=str1[j];//each row up
if ( Str1[j]==0 && (j==0 | | | str1[j-1]!=0))
{
res+=cur+to;
Cur= "";
}
}
if (Cur.c_str ()!= "")
res+=cur;//last part
delete[] str1;
return res;
}
int main ()
{
string str,from,to;
Getline (CIN,STR);
Getline (cin,from);
Getline (cin,to);
Cout<<replace (str,from,to);
return 0;
}
*/
/*
7. Statistical string EG:AAABBADDDFFC A_3_b_2_a_1_d_3_f_2_c_1
#include <sstream>//string Stream, String
getcountstring (String str)
{
const char *STR1=STR.C_ STR ();
String res= "";
Res+=str1[0];//char the string directly assigns the
StringStream SS;
string tmp;
int num=1;
for (int i=1;i<str.length (); i++)
{
if (str1[i]!=str1[i-1))
{
ss.clear ();
Ss<<num;//int string
ss>>tmp;
res+= "_" +tmp+ "_" +str1[i];
num=1;
}
else
num++;
}
Ss.clear ();
ss<<num;
ss>>tmp;
Return res+= "_" +TMP;
}
int main ()
{
string str;
Getline (CIN,STR);
Cout<<getcountstring (str);
return 0;
}
*/
/*
8. A given index returns the index of the original character in the statistics string eg:a_1_b_100 index=50 returns b
Char Getcharat (String Str,int Index)
{
const char *str1=str.c_str ();
BOOL stage=true;//is currently traversing a character or a numeric flag, true character
int num=0,sum=0;
Char cur;
for (int i=0;i<str.length (); i++)
{
if (str1[i]== ' _ ')
stage=!stage;
else if (stage)
{
sum+=num;
if (sum>index) return
cur;
num=0;
Cur=str1[i];
}
else num=num*10+str1[i]-' 0 ';
}
Return Sum+num>index? cur:0;
}
int main ()
{
string str;
int index;
cin>>index;
Cin.ignore ();
Getline (CIN,STR);
Cout<<getcharat (str,index);
return 0;
}
*/
/*
9. Judge whether all characters in the character array appear only once, without stipulating space complexity
BOOL Isuniquel (const char *buf,int len)
{
bool map[256]={0};
for (int i=0;i<len;i++)
{
if (Map[buf[i]])
{
cout<<map[buf[i]];
return false;
}
map[buf[i]]=true;
}
return true;
}
int main ()
{
string str;
Getline (CIN,STR);
Isuniquel (Str.c_str (), Str.length ()) ==true? cout<< "true": cout<< "false";
return 0;
}
*/
/*
10. In the above question requires the space complexity to be O (1) time complexity to be as low as possible
Idea: Sort after find whether more than 1, key in the selection with sorting algorithm, heap sort O (1) space, Time O (NLOGN)
void Heapadjust (char a[],int s,int N)
{
//adjusted to small Gan, from childhood to large
int rc=a[s];
for (int j=2*s;j<=n;j*=2)
{
if (j<n && a[j]>a[j+1])//To determine the left and right child size
j + +;
if (Rc<=a[j]) break
;
A[S]=A[J];
s=j;
}
A[S]=RC;
}
Step one: Build the first heap
void Creatheap (char a[],int N)
{
//Keng Gen for
(int i=n/2;i>0;i--)
heapadjust (a,i , n);
}
Consolidate
void Heapsort (char a[],int N)
{
creatheap (a,n);//First step, set up initial heap for
(int i=n;i>1;i--)
{
int x=a[1];//heap top with last element interchange
A[1]=a[i];
a[i]=x;
Heapadjust (a,1,i-1);
}
Lookup Method
bool IsUnique2 (char a[],int N)
{
//call sort algorithm
heapsort (a,n);
for (int i=1;i<n;i++)
if (A[i]==a[i-1]) return
false;
return true;
}
int main ()
{
string str;
Getline (CIN,STR);
Char *a=new char[str.length () +1];
strcpy (A,str.c_str ());
cout<< (IsUnique2 (A,str.length ()) ==true? "True": "false");
Delete[] A;
return 0;
}
*/
/*
11. Find the position of a string in a small to large sort but containing a null string, Eg:{a,null,b,c,null,d,c.} Find C appears leftmost position 3
Train of thought: Two-point search, because it's already orderly.
The following program is in fact wrong: The following meaning is from the sort but contains a space in the string to find the position of the character (space and Null is not the same, the space is ' asc=32,null is the pointer its value is 0, the Terminator is ' asc=0 ')
So the real program here should be an array of strings
int GetIndex (string Str,char c)
{
int res=-1;
int left=0;
int Right=str.length ()-1;
int mid=0;
int i=0;
const char *BUF=STR.C_STR ();
while (Left<=right)
{
mid= (left+right)/2;
if (buf[mid]!= ')
{
if (buf[mid]==c)//Just is, not necessarily the most left, but also to the left to find
{
res=mid;
right=mid-1;
}
else if (buf[mid]<c)//on the right
left=mid+1;
else right=mid-1; On the left
}
else
{
i=mid;
while (str[i]== ' &&--i>=left);//Turn Left
if (I<left | | str[i]<c)//If left is empty or the leftmost character in the left is <c, From the right to find
left=mid+1;
else//On the left of
{
res= (str[i]==c i:res);
Right=i-1
}
}} return res;
}
int main ()
{
string str;
char c;
Getline (CIN,STR);
cin>>c;
Cout<<getindex (str,c);
return 0;
}
*/
/*
11.1 String adjustment and substitution: replace the space with%20, note that the space is a byte,%20 is three bytes, the array cannot be crossed
Idea: Iterate through it, see how many characters are in the array, how many spaces, and then recalculate the replaced array length, and replace the conversion position from right to left
void replace (char buf[])
{
int len=0;//buf how many characters
int num=0;//space number
int size=0;//replace length for
(len=0; buf[len]!= ' len++ ';
if (buf[len]== ')
num++;
size=len+2*num-1;
for (int i=len-1;i>=0;i--)
{
if (buf[i]!= ')
buf[size--]=buf[i];
else {buf[size--]= ' 0 '; buf[size--]= ' 2 '; buf[size--]= '% ';}
}} int main ()
{
string str;
Getline (CIN,STR);
The Char *buf=new char[256];//is represented here with a fixed length large enough
strcpy (Buf,str.c_str ());
Replace (BUF);
cout<<buf;
Delete[] BUF;
return 0;
}
*/
/*
11.2 String adjustment and substitution: characters only numbers and *, move all * to the left of the number, and cannot change the order before the number eg12**45 **1245
Train of thought: from right to left, is the number from right to left, not afraid to cover the *, no number of the rest of the area after the copy into the * is good, because not the number is *
void Modify (char buf[],int len)
{
int j=len-1;
for (int i=j;i>=0;i--)
{
if (buf[i]!= ' * ')
buf[j--]=buf[i];
}
while (j>=0)
buf[j--]= ' * ';
}
int main ()
{
string str;
Getline (CIN,STR);
Char *buf=new char[str.length () +1];
strcpy (Buf,str.c_str ());
Modify (Buf,str.length ());
cout<<buf;
Delete[] BUF;
return 0;
}
*/