class Solution {
public:
/**
* 反转字符串
* @param str string字符串
* @return string字符串
*/
string solve(string str) {
// write code here
string t="";
for(int i=str.size()-1;i>=0;i--)
t+=str[i];
return t;
}
};
class Solution {
public:
/**
* 进制转换
* @param M int整型 给定整数
* @param N int整型 转换到的进制
* @return string字符串
*/
string solve(int M, int N) {
// write code here
int ls=abs(M);
string s;
stack<char>st;
while(ls)
{
if(ls%N<10)
st.push(ls%N+'0');
else if(ls%N==10)
st.push('A');
else if(ls%N==11)
st.push('B');
else if(ls%N==12)
st.push('C');
else if(ls%N==13)
st.push('D');
else if(ls%N==14)
st.push('E');
else if(ls%N==15)
st.push('F');
ls/=N;
}
if(M<0)
st.push('-');
while(!st.empty())
{
s+=st.top();
st.pop();
}
return s;
}
};
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 计算两个数之和
* @param s string字符串 表示第一个整数
* @param t string字符串 表示第二个整数
* @return string字符串
*/
string solve(string s, string t) {
// write code here
int cd1=s.size();
int cd2=t.size();
string ans;
while(cd1<cd2)
{
s="0"+s;
cd1++;
}
while(cd1>cd2)
{
t="0"+t;
cd2++;
}
// cout<<t<<endl;
int C=0;
for(int i=cd1-1; i>=0; i--)
{
int tmp=(s[i]-'0'+t[i]-'0'+C);
ans+=char(tmp%10+'0');
C=tmp/10;
}
if(C)
ans+=(C+'0');
reverse(ans.begin(),ans.end());
return ans;
}
};
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
int getLongestPalindrome(string A) {
// write code here
int n=A.size();
string s="#";
for(int i=0;i<n;i++){
s+=A[i];
s+="#";
}
int ans=0;
int R=0;
int L=0;
int mid=0;
vector<int>Len(s.length());
for(int i=0;i<s.length();i++){
if(i>=R){
int len=0;
while((i-len>=0&&i+len<=s.length())&&s[i-len]==s[i+len])
len++;
Len[i]=len-1;
}
else{
int i_=mid-(i-mid);
int L_=i_-Len[i_];
if(L_>L)//在边界内
Len[i]=Len[i_];
else if(L_<L)//在边界外
Len[i]=R-i;
else{
int len=R-i+1;
while((i-len>=0&&i+len<=s.length())&&s[i-len]==s[i+len])
len++;
Len[i]=len-1;
}
}
if(i+Len[i]>R){
mid=i;
R=i+Len[i];
L=i-Len[i];
}
ans=max(ans,Len[i]*2+1);
}
return ans/2;
}
};
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算模板串S在文本串T中出现了多少次
* @param S string字符串 模板串
* @param T string字符串 文本串
* @return int整型
*/
int kmp(string S, string T) {
// write code here
vector<int> next(S.length()+1);
next[0]=next[1]=0;
for(int i=1;i<S.length();i++){
int j=next[i];
while(S[j]!=S[i]&&j!=0)
j=next[j];
if(S[j]==S[i])
next[i+1]=j+1;
else
next[i+1]=0;
}
int j=0;
int ans=0;
for(int i=0;i<T.length();i++){
while(T[i]!=S[j]&&j!=0)
j=next[j];
if(T[i]==S[j])
j++;
if(j==S.length()){
ans++;
j=next[j];
}
}
return ans;
}
};