【CodeForces - 122B 】Lucky Substring (字符串,水题)
題干:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits?4?and?7. For example, numbers?47,?744,?4?are lucky and?5,?17,?467?are not.
One day Petya was delivered a string?s, containing only digits. He needs to find a string that
- represents a lucky number without leading zeroes,
- is not empty,
- is contained in?s?as a substring the maximum number of times.
Among all the strings for which the three conditions given above are fulfilled, Petya only needs the lexicographically minimum one. Find this string for Petya.
Input
The single line contains a non-empty string?s?whose length can range from?1?to?50, inclusive. The string only contains digits. The string can contain leading zeroes.
Output
In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes).
Examples
Input
047Output
4Input
16Output
-1Input
472747Output
7Note
The lexicographical comparison of strings is performed by the < operator in the modern programming languages. String?x?is lexicographically less than string?yeither if?x?is a prefix of?y, or exists such?i?(1?≤?i?≤?min(|x|,?|y|)), that?xi?<?yiand for any?j?(1?≤?j?<?i)?xj?=?yj. Here?|a|?denotes the length of string?a.
In the first sample three conditions are fulfilled for strings "4", "7" and "47". The lexicographically minimum one is "4".
In the second sample?s?has no substrings which are lucky numbers.
In the third sample the three conditions are only fulfilled for string "7".
題目大意:
? ?給你一個可能有前導零的數字,讓你找一個子串,滿足三個條件,其中第三個是,包含最大數量的幸運數字,也就是說,4和7誰多 就要包含誰,又因為是字典序最小的,所以就是那個數字本身嘍。
解題報告:
? ? 讀懂題目就好了,另外就是注意ifelse啥的之類的別落下情況。先排除-1 的情況,然后再else其他的情況。
? ?就是題目不太好懂。
AC代碼:
#include<bits/stdc++.h> #define ll long long using namespace std; const int MAX = 1e5 +5; int main() {string s;cin>>s;ll len = s.length(),n4=0,n7=0;for(int i = 0; i<len; i++) {if(s[i] == '4') n4++;if(s[i] == '7') n7++;}if(n4 == 0 && n7 == 0) puts("-1");else {if(n7 > n4) puts("7");else puts("4");}return 0 ;}?
總結
以上是生活随笔為你收集整理的【CodeForces - 122B 】Lucky Substring (字符串,水题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡怎么代还 小心掉入“黑中介”的陷阱
- 下一篇: 什么是地方政府债券?如何参与地方政府债券