Gym 100712I Bahosain and Digits(枚举)
来源:互联网 发布:什么是协同过滤算法? 编辑:程序博客网 时间:2024/04/30 12:38
/*********
Gym 100712I
Bahosain and Digits
Bahosain has a string of digits. He is going to perform the following operation on the string as many times as
needed to make all digits in the string the same.
In one move, he can choose a substring of length Kand change each digit in this substring to it’s next digit,
the digit that goes after ‘9’is ‘0’.
For example, applying the operation on the string 905 will change it to 016.
Your task is to determine the maximum K?such that it is possible to make all digits in the string equal using
the described operation.
Input
The first line of input contains an integer N (1 ≤ T ≤ 128)that represents the number of test cases.
Each test case contains a string of no more than 250 digits (0 - 9).
Each string contains at least two different digits.
Output
For each test case print a single line with the maximum possible K.
Sample Input
3
04
651
0552
Sample Output
1
2
3
Note
In the last test case, when K= 3,Bahosain can perform the operation on the substring 552 five times to get
the string 0007,then perform the operation again on the substring 000seven times to get 7777.
题目大意就是给一个序列,每次可以转动一个区间,使这个区间的值加1,9则变成0,目标是是这个序列的值最终一样,求每次转动的区间的最大长度。
可以先从大到小枚举区间长度ans,同时枚举最终整个序列的值k,在循环体内计算每一位要转动的次数,同时对后(ans-1)位进行特殊判断,如果当前情况合法,则后(ans-1)位需要转动的次数都为0。循环体内不能每次修改ans个值,会造成超时,所以循环体内的时间复杂度要是O(n)。因此我们可以定义一个数组change,change[i]表示经过前i-1次转动第i个位置要转动为k所要转动的次数,再另外一个变量sum表示对第i个位置造成影响的转动次数,即从i-ans位到i-1位转动次数之和。
*********/
Gym 100712I
Bahosain and Digits
Bahosain has a string of digits. He is going to perform the following operation on the string as many times as
needed to make all digits in the string the same.
In one move, he can choose a substring of length Kand change each digit in this substring to it’s next digit,
the digit that goes after ‘9’is ‘0’.
For example, applying the operation on the string 905 will change it to 016.
Your task is to determine the maximum K?such that it is possible to make all digits in the string equal using
the described operation.
Input
The first line of input contains an integer N (1 ≤ T ≤ 128)that represents the number of test cases.
Each test case contains a string of no more than 250 digits (0 - 9).
Each string contains at least two different digits.
Output
For each test case print a single line with the maximum possible K.
Sample Input
3
04
651
0552
Sample Output
1
2
3
Note
In the last test case, when K= 3,Bahosain can perform the operation on the substring 552 five times to get
the string 0007,then perform the operation again on the substring 000seven times to get 7777.
题目大意就是给一个序列,每次可以转动一个区间,使这个区间的值加1,9则变成0,目标是是这个序列的值最终一样,求每次转动的区间的最大长度。
可以先从大到小枚举区间长度ans,同时枚举最终整个序列的值k,在循环体内计算每一位要转动的次数,同时对后(ans-1)位进行特殊判断,如果当前情况合法,则后(ans-1)位需要转动的次数都为0。循环体内不能每次修改ans个值,会造成超时,所以循环体内的时间复杂度要是O(n)。因此我们可以定义一个数组change,change[i]表示经过前i-1次转动第i个位置要转动为k所要转动的次数,再另外一个变量sum表示对第i个位置造成影响的转动次数,即从i-ans位到i-1位转动次数之和。
*********/
#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int MAXN=260;char str[MAXN];int num[MAXN],n;int main(){#ifndef ONLINE_JUDGE freopen("in.txt","r",stdin);#endif // ONLINE_JUDGE int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%s",str); n=strlen(str); for(int i=0;i<n;i++) { num[i]=str[i]-'0'; } int ans; for(ans=n;ans>=1;ans--) { int flag; for(int k=0;k<10;k++) { int sum=0,change[MAXN]={0}; for(int i=0;i<n-(ans-1);i++)//循环到n-(ans-1),最后(ans-1)位要进行特殊判断 { if(i-ans>=0)//防止下标越界 { sum-=change[i-ans]; } change[i]=((k+10)-(num[i]+sum)%10)%10;//k+10为了防止k比num[i]小 sum+=change[i]; } flag=1; for(int i=n-(ans-1);i<n;i++) { if(i-ans>=0) { sum-=change[i-ans]; } change[i]=((k+10)-(num[i]+sum)%10)%10; if(change[i]!=0)//如果当前情况合法,则后(ans-1)位都不需要转动 { flag=0; break; } } if(flag) { printf("%d\n",ans); break; } } if(flag) break; } } return 0;}
1 0
- Gym 100712I Bahosain and Digits(枚举)
- GYM 100712 I.Bahosain and Digits(枚举)
- 2015 ACM Amman Collegiate Programming Contest I.Bahosain and Digits【思维+暴力枚举】
- Bahosain and Digits
- Bahosain and Digits
- 开关翻转 Gym100712I Bahosain and Digits
- Multiplying Digits Gym
- Gym 100338I TV show (dfs枚举)
- GYM 100712 G.Heavy Coins(枚举)
- gym 101170 NWERC 2016 I Iron and Coal
- Gym 100531 D Digits 【暴力】
- Gym 100712G Heavy Coins(二进制枚举子集)
- Gym 100531D Digits(暴力)
- Gym 101158B Quality of Check Digits
- CodeForces Gym 100735I
- Mirrored String I Gym
- Gym 101147.I
- Gym 101164.I
- 《IOS编程》控制逻辑课后练习 学习手记
- SpringMVC 用注解Annotation驱动的IoC功能@Autowired @Component
- 初识数据库
- Gym 100712G Heavy Coins(DFS)
- “儿童”计算器
- Gym 100712I Bahosain and Digits(枚举)
- Majority Number II
- 原型模式
- POJ 1753 Flip Game(BFS)
- Scala:基于trait的多重继承构造器的执行顺序、基于trait的AOP实践
- C++中模板类使用友元模板函数
- 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
- linux下apache绑定多个域名
- 设计模式--命令模式