Skip to content

Commit

Permalink
Create Uva1261_String Popping.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Huixxi authored Jan 24, 2020

Verified

This commit was signed with the committer’s verified signature.
TimidRobot Timid Robot Zehta
1 parent eca9b16 commit 1a9542a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Week13-TSP旅行商&区间DP/Uva1261_String Popping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <bits/stdc++.h>
using namespace std;

map<string, int> mm;

int dfs(string& s) {
if(s == "")
return 1;
if(s.length() == 1)
return 0;
auto it = mm.find(s);
if(it != mm.end())
return it->second;
string sub;
int i, j;
int& res = mm[s];
for(i = 0; i < s.length(); ++i) {
for(j = i; j < s.length(); ++j) {
if(s[i] != s[j])
break;
}
if(j >= i + 2) {
sub = s.substr(0, i) + s.substr(j);
res |= dfs(sub);
if(res)
return res;
i = j - 1;
}
}
return res;
}

int main() {
int n;
cin >> n;
string s;
while(n--) {
cin >> s;
cout << dfs(s) << endl;
}
return 0;
}

0 comments on commit 1a9542a

Please sign in to comment.