Skip to content

Commit 27a32ea

Browse files
committed
2017.05.03
1 parent bf8e85e commit 27a32ea

File tree

6 files changed

+311
-0
lines changed

6 files changed

+311
-0
lines changed

10.1.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//给一个字符串,分割使得分割后的所有子串都为回文//
2+
#include<iostream>
3+
#include<string>
4+
#include<algorithm>
5+
#include<vector>
6+
using namespace std;
7+
//深搜
8+
class Solution{
9+
public:
10+
vector<vector<string>> partition(string s){
11+
vector<vector<string>> result;
12+
vector<string> cur;
13+
dfs(s,result,cur,0,1);
14+
return result;
15+
}
16+
void dfs(string& s,vector<vector<string>>& result,vector<string>& cur,int prev,int start){
17+
auto isPalindrome=[](const string& s){
18+
auto begin=s.begin();
19+
auto end=std::prev(s.end());
20+
while(begin<=end){
21+
if(*begin!=*end)
22+
return false;
23+
begin++;
24+
end--;
25+
}
26+
return true;
27+
};
28+
if(start==s.size()){
29+
if(isPalindrome(s.substr(prev,start-prev))){
30+
cur.push_back(s.substr(prev,start-prev));
31+
result.push_back(cur);
32+
cur.pop_back();
33+
}
34+
return;
35+
}
36+
dfs(s,result,cur,prev,start+1);
37+
if(isPalindrome(s.substr(prev,start-prev))){
38+
cur.push_back(s.substr(prev,start-prev));
39+
dfs(s,result,cur,start,start+1);
40+
cur.pop_back();
41+
}
42+
}
43+
};
44+
//DP
45+
class Solution1{
46+
public:
47+
vector<vector<string>> partition(string s){
48+
const int n=s.size();
49+
bool p[n][n];
50+
fill_n(&p[0][0],n*n,0);
51+
for(int i=n-1;i>=0;i--)
52+
for(int j=i;j<n;j++)
53+
p[i][j]=s[i]==s[j]&&(j-i<2||p[i+1][j-1]);
54+
vector<vector<string>> result[n];
55+
for(int i=n-1;i>=0;i--)
56+
for(int j=i;j<n;j++)
57+
if(p[i][j]){
58+
if(j<n-1){
59+
for(auto str:result[j+1]){
60+
str.insert(str.begin(),s.substr(i,j-i+1));
61+
result[i].push_back(str);
62+
}
63+
}else{
64+
result[i].push_back(vector<string>{s.substr(i,j-i+1)});
65+
}
66+
}
67+
return result[0];
68+
}
69+
};
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
int main(){
89+
Solution s;
90+
string str="aab";
91+
auto result=s.partition(str);
92+
for(auto i:result){
93+
for(auto j:i)
94+
cout<<j<<' ';
95+
cout<<endl;
96+
}
97+
};
98+
99+
100+
101+
102+

10.2.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//从一个m*n的数组左上角走到右下角有多少种走法//
2+
#include<iostream>
3+
#include<vector>
4+
#include<algorithm>
5+
#include<stack>
6+
using namespace std;
7+
//深搜
8+
class Solution1{
9+
int uniquePaths(int m,int n){
10+
if(m<0||n<0)return 0;
11+
if(m==1||n==1)return 1;
12+
return uniquePaths(m-1,n)+uniquePaths(m,n-1);
13+
}
14+
};
15+
16+
//深搜,加备忘录可以加速
17+
class Solution2{
18+
int uniquePaths(int m,int n){
19+

9.1.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//给两个单词和一个字典,找到最短变换距离.每次只能变一个字母,必须按照字典中的路径走
2+
#include<iostream>
3+
#include<queue>
4+
#include<algorithm>
5+
#include<unordered_set>
6+
using namespace std;
7+
struct state_t{
8+
string word;
9+
int level;
10+
state_t(){word="";level=0;}
11+
state_t(const string& word,int level){
12+
this->word=word;
13+
this->level=level;
14+
}
15+
bool operator==(const state_t &other)const{
16+
return this->word==other.word;
17+
}
18+
};
19+
20+
namespace std{
21+
template<> struct hash<state_t>{
22+
public:
23+
size_t operator()(const state_t& s)const {
24+
return str_hash(s.word);
25+
}
26+
private:
27+
hash<string>str_hash;
28+
};
29+
}
30+
31+
32+
class Solution{
33+
public:
34+
int ladderLength(const string& start,const string& end,const unordered_set<string>&dict){
35+
queue<state_t> q;
36+
unordered_set<state_t> visited;//用于判重
37+
auto state_is_valid=[&](const state_t& s){
38+
return dict.find(s.word)!=dict.end()||s.word==end;
39+
};
40+
auto state_is_target=[&](const state_t &s){return s.word==end;};
41+
auto state_extend=[&](const state_t& s){
42+
unordered_set<state_t> result;
43+
for(int i=0;i<s.word.size();i++){
44+
state_t new_state(s.word,s.level+1);
45+
for(char c='a';c<='z';c++){
46+
if(c==new_state.word[i])continue;
47+
swap(c,new_state.word[i]);
48+
if(state_is_valid(new_state)&&visited.find(new_state)==visited.end())
49+
result.insert(new_state);
50+
swap(c,new_state.word[i]);
51+
}
52+
}
53+
return result;
54+
};
55+
state_t start_state(start,0);
56+
q.push(start_state);
57+
visited.insert(start_state);
58+
while(!q.empty()){
59+
const auto state=q.front();
60+
q.pop();
61+
if(state_is_target(state))
62+
return state.level+1;
63+
const auto& next_states=state_extend(state);
64+
for(const auto& new_state:next_states){
65+
q.push(new_state);
66+
visited.insert(new_state);
67+
}
68+
return 0;
69+
}
70+
}
71+
};
72+
int main(){
73+
}
74+
75+
76+
77+
78+
79+
80+

9.2.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<unordered_set>
4+
#include<unordered_map>
5+
#include<algorithm>
6+
using namespace std;
7+
struct state_t{
8+
9+
int main(){
10+
}

9.3.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//一个2维数组只有"X"和'O',如果'O'区域被x包围则需要被改成'+'
2+
//思路:用广搜,起点为边上元素向内部搜索
3+
#include<iostream>
4+
#include<vector>
5+
#include<algorithm>
6+
#include<queue>
7+
using namespace std;
8+
class Solution{
9+
public:
10+
void solve(vector<vector<char>> &board){
11+
if(board.empty())return;
12+
int m=board.size();
13+
int n=board[0].size();
14+
for(int i=0;i<n;i++){
15+
bfs(board,0,i);
16+
bfs(board,m-1,i);
17+
}
18+
for(int i=1;i<m-1;i++){
19+
bfs(board,i,0);
20+
bfs(board,i,n-1);
21+
}
22+
for(int i=0;i<m;i++){
23+
for(int j=0;j<n;j++){
24+
if(board[i][j]=='O')
25+
board[i][j]=='X';
26+
else if(board[i][j]=='+')
27+
board[i][j]=='O';
28+
}
29+
}
30+
}
31+
32+
void bfs(vector<vector<char>> &board,int i,int j){
33+
typedef pair<int,int> state_t;
34+
queue<state_t>q;
35+
const int m=board.size();
36+
const int n=board[0].size();
37+
auto state_is_valid=[&](const state_t& s){
38+
const int x=s.first;
39+
const int y=s.second;
40+
if(0<=x<=m-1&&0<=y<=n-1&&board[x][y]=='O')
41+
return true;
42+
else return false;
43+
};
44+
auto state_extend=[&](const state_t& s){
45+
vector<state_t> new_state_ts={state_t(s.first-1,s.second),
46+
state_t(s.first,s.second-1),
47+
state_t(s.first+1,s.second),
48+
state_t(s.first,s.second+1)};
49+
vector<state_t>result;
50+
for(int i=0;i<new_state_ts.size();i++)
51+
if(state_is_valid(new_state_ts[i])){
52+
board[new_state_ts[i].first][new_state_ts[i].second]=='+';
53+
result.push_back(new_state_ts[i]);
54+
}
55+
return result;
56+
};
57+
state_t start(i,j);
58+
if(state_is_valid(start)){
59+
board[i][j]=='+';
60+
q.push(start);
61+
}
62+
while(!q.empty()){
63+
const auto p=q.front();
64+
q.pop();
65+
for(auto next_state_t:state_extend(p))
66+
q.push(next_state_t);
67+
}
68+
}
69+
70+
71+
72+
73+
};
74+
int main(){}

baidu5.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<algorithm>
4+
#include<memory.h>
5+
using namespace std;
6+
int all_array(int n,int k){
7+
vector<vector<int>> count(n,vector<int>(n+1,0));
8+
for(int i=1;i<n+1;i++)
9+
count[0][i]=1;
10+
for(int i=1;i<k+1;i++){
11+
for(int j=i+1;j<n+1;j++){
12+
count[i][j]=(count[i][j-1]*(i+1)+count[i-1][j-1]*(j-i))%2017;
13+
}
14+
}
15+
return count[k][n];
16+
}
17+
int main(){
18+
int n;
19+
cin>>n;
20+
int k;
21+
cin>>k;
22+
cout<<all_array(n,k);
23+
}
24+
25+
26+

0 commit comments

Comments
 (0)