| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
数据弱爆了,STL满天飞都0ms#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;
void getState(int i, int state[26], bool yjdd[26], int adjNum[26], int adjList[26][26]){
if(yjdd[i]) return;
for(int j = 0; j < adjNum[i]; j++) getState(adjList[i][j], state, yjdd, adjNum, adjList);
for(int j = 0; j < adjNum[i]; j++) {
state[i] |= state[adjList[i][j]];
state[i] |= (1 << adjList[i][j]);
}
yjdd[i] = 1;
}
void dfs(int &st, int yygs, int zgs, int stk[26], char zm[26], int state[26]){
if(yygs == zgs){
for(int i = 0; i < zgs; i++){
printf("%c", zm[stk[i]]);
}
printf("\n");
return;
}
for(int i = 0; i < zgs; i++){
int p2i = 1 << i;
if((st & p2i) != 0) continue;
if((state[i] & st) != state[i]) continue;
st += p2i;
stk[yygs] = i;
dfs(st, yygs+1, zgs, stk, zm, state);
st -= p2i;
}
}
int main() {
string s;
while(getline(cin, s)){
string c;
getline(cin, c);
int has[26] = {0};
int ls = s.length();
for(int i = 0; i < ls; i++){
if(s[i] != ' ') has[s[i]-'a'] = 1;
}
int cnt = 0;
int idx[26];
char zm[26];
for(int i = 0; i < 26; i++){
if(has[i]){
zm[cnt] = i + 'a';
idx[i] = cnt;
cnt++;
}
}
int state[26] = {0};
bool yjdd[26] = {0};
int adjNum[26] = {0};
int adjList[26][26];
string cPrep = "";
int lc = c.length();
for(int i = 0; i < lc; i++){
if(c[i] != ' ') cPrep += c[i];
}
int lc_ = cPrep.length();
for(int i = 0; i < lc_; i+=2){
int idx1 = idx[cPrep[i]-'a'], idx2 = idx[cPrep[i+1]-'a'];
adjList[idx2][adjNum[idx2]] = idx1;
adjNum[idx2]++;
}
for(int i = 0; i < cnt; i++){
getState(i, state, yjdd, adjNum, adjList);
}
//for(int i = 0; i < cnt; i++) cout << state[i] << endl;
int st = 0;
int stk[26];
dfs(st, 0, cnt, stk, zm, state);
printf("\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator