Vasya and Golden Ticket CodeForces - 1030C
https://vjudge.net/contest/356807#problem/A
题意:
给一段数字,问是否能拆分成若干段,使得各段的数字和相等
思路:
暴力 搜索
串的长度最大100,单个段数字和最大为100 * 9 = 900,以单个段数字和为线索进行搜索
对每个单段长度 i ∈(1 , 900),尝试分段,能分尽即为有解
时间复杂度O(N),N为最小的单段数字和
最坏情况下进行900 * 100 = 90000次判断
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<bitset> 6 #include<cassert> 7 #include<cctype> 8 #include<cmath> 9 #include<cstdlib> 10 #include<ctime> 11 #include<deque> 12 #include<iomanip> 13 #include<list> 14 #include<map> 15 #include<queue> 16 #include<set> 17 #include<stack> 18 #include<vector> 19 #include <vector> 20 #include <iterator> 21 #include <utility> 22 #include <sstream> 23 #include <limits> 24 #include <numeric> 25 #include <functional> 26 using namespace std; 27 #define gc getchar() 28 #define mem(a) memset(a,0,sizeof(a)) 29 //#define sort(a,n,int) sort(a,a+n,less<int>()) 30 31 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 32 33 typedef long long ll; 34 typedef unsigned long long ull; 35 typedef long double ld; 36 typedef pair<int,int> pii; 37 typedef char ch; 38 typedef double db; 39 40 const double PI=acos(-1.0); 41 const double eps=1e-6; 42 const ll mod=1e9+7; 43 const int inf=0x3f3f3f3f; 44 const int maxn=1e5+10; 45 const int maxm=100+10; 46 47 char c[100] = {0}; 48 int num[100] = {0}; 49 int main() 50 { 51 int length = 0; 52 cin >> length; 53 cin >> c; 54 for(int i = 0;i<length;i++) 55 { 56 num[i]=c[i]-'0'; 57 //cout << num[i]; 58 } 59 bool flag = 0; 60 for(int i = 0;i<=900;i++) 61 { 62 int sum = 0 , counter = 0; 63 for (int j = 0;j<length;j++) 64 { 65 sum += num[j]; 66 if(sum == i) 67 { 68 sum = 0; 69 counter++; 70 } 71 else if(sum>i) break; 72 } 73 if (sum == 0 && counter > 1) 74 { 75 flag = 1; 76 //cout<<"!"<<endl; 77 } 78 } 79 if(flag) 80 { 81 cout << "YES" << endl; 82 } 83 else 84 { 85 cout << "NO" << endl; 86 } 87 return 0; 88 }