NC53384. Numbers
描述
输入描述
The input consists of several test cases and is terminated by end-of-file.
Each test case contains a string s.
*
* There are at most 100 test cases.
输出描述
For each test case, print an integer which denotes the result.
示例1
输入:
999 233333 0123456789
输出:
2 0 55
C++14(g++5.4) 解法, 执行用时: 53ms, 内存消耗: 376K, 提交时间: 2020-08-21 19:13:35
#include<bits/stdc++.h> using namespace std; bool b[101]; int len; long long ans; string s; void dfs(int p) { int t=s[p]-'0'; if(p==len){ans++;return;} if(b[t]){b[t]=0;dfs(p+1);b[t]=1;} t=(s[p]-'0')*10+s[p+1]-'0'; if(b[t]&&p+2<=len&&s[p]!='0'){b[t]=0;dfs(p+2);b[t]=1;} } int main() { int i,j,k,t; while(cin>>s) { len=s.length();ans=0; for(i=0;i<=99;++i) b[i]=1; dfs(0); cout<<ans<<endl; } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 57ms, 内存消耗: 376K, 提交时间: 2020-08-21 18:45:53
#include<bits/stdc++.h> using namespace std; bool b[101]; int len; long long ans; string s; void dfs(int p) { int t=s[p]-'0'; if(p==len){ans++;return;} if(b[t]){b[t]=0;dfs(p+1);b[t]=1;} t=(s[p]-'0')*10+s[p+1]-'0'; if(b[t]&&p+2<=len&&s[p]!='0'){b[t]=0;dfs(p+2);b[t]=1;} } int main() { int i,j,k,t; while(cin>>s) { len=s.length();ans=0; for(i=0;i<=99;++i) b[i]=1; dfs(0); cout<<ans<<endl; } return 0; }