题目:题目
全排列,暴力
第一种:对子序暴力,由题目可知,s<=3000,子序复杂度C(3000,9),根据组合数计算公式,复杂度大概是稍小于3000的阶乘
第二种哪个:对字典序暴力,把字典序全排列,复杂度是9的阶乘,还可以接受,继续往下考虑,遍历每一个字符串复杂度位s.size(),最大也就是3000,那么总时间复杂度位O(3000*9!),交的第一遍过了80%后TLE. 不死心,又点了一边提交,竟然卡过去了。。。
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<ctime>
using namespace std;
vector<char> dic;
int main(void) {
#ifndef ONLINE_JUDGE
freopen("E:\\input.txt","r",stdin);
#endif
ios::sync_with_stdio(false);
for(int i=0; i<9; i++)
dic.push_back('a'+i);
string s;
cin>>s;
int res=0;
do {
int cnt=0,now=0,len=s.size();
for(auto ch:dic) {//找子序
while(now<len) {
if(ch==s[now]) {
cnt++;
break;
}
now++;
}
if(now==len)
break;
}
if(cnt==9)
res++;
} while(next_permutation(dic.begin(),dic.end()));//全排列字典序
cout<<res<<endl;
#ifndef ONLINE_JUDGE
printf("My Time:%.3lfms\n",(double)clock()/CLOCKS_PER_SEC);
#endif
return 0;
}