Problem Statement
Takahashi is a cashier.
There is a cash register with 1111 keys: 00
, 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, and 9
. The cash register initially displays 00. Whenever he types the key 00
, the displayed number is multiplied by 100100; whenever he types one of the others, the displayed number is multiplied by 1010, and then added by the number written on the key.
Takahashi wants the cash register to display an integer SS. At least how many keystrokes are required to make it display SS?
Constraints
- 1\leq S\leq 10^{100000}1≤S≤10的100000次方
- SS is an integer.
Input
The input is given from Standard Input in the following format:
SS
Output
Print the answer in a line.
Sample 1
Inputcopy | Outputcopy |
---|---|
40004 | 4 |
For example, the following four keystrokes make the cash register display 4000440004. Initially, the cash register displays 00.
- Type the key
4
. It now displays 44. - Type the key
00
. It now displays 400400. - Type the key
0
. It now displays 40004000. - Type the key
4
. It now displays 4000440004.
He cannot make it display 4000440004 with three or fewer keystrokes, so 44 should be printed.
Sample 2
Inputcopy | Outputcopy |
---|---|
1355506027 | 10 |
Sample 3
Inputcopy | Outputcopy |
---|---|
10888869450418352160768000001 | 27 |
Note that SS may not fit into a 6464-\operatorname{bit}bit integer type.
思路:数字过多,很明显要用字符串解决。每位数字占一位,如果有两个00一起也只算一位。用这个思路就能解决问题
#include<stdio.h>
#include<string.h>
int main()
{
char str[100000];
int i=0,ans=0;
gets(str);
for( ;i<strlen(str);i++){
if(str[i]!='0'){ans++;continue;}
else if(str[i+1]=='0'){ans++;i++;continue;}
else ans++;
}printf("%d\n",ans);
}
朋友
题目背景
小明在 A 公司工作,小红在 B 公司工作。
题目描述
这两个公司的员工有一个特点:一个公司的员工都是同性。
A 公司有 NN 名员工,其中有 PP 对朋友关系。B 公司有 MM 名员工,其中有 QQ 对朋友关系。朋友的朋友一定还是朋友。
每对朋友关系用两个整数 (X_i,Y_i)(Xi,Yi) 组成,表示朋友的编号分别为 X_i,Y_iXi,Yi。男人的编号是正数,女人的编号是负数。小明的编号是 11,小红的编号是 -1−1。
大家都知道,小明和小红是朋友,那么,请你写一个程序求出两公司之间,通过小明和小红认识的人最多一共能配成多少对情侣(包括他们自己)。
输入格式
输入的第一行,包含 44 个空格隔开的正整数 N,M,P,QN,M,P,Q。
之后 PP 行,每行两个正整数 X_i,Y_iXi,Yi。
之后 QQ 行,每行两个负整数 X_i,Y_iXi,Yi。
输出格式
输出一行一个正整数,表示通过小明和小红认识的人最多一共能配成多少对情侣(包括他们自己)。
输入输出样例
输入 #1复制
4 3 4 2 1 1 1 2 2 3 1 3 -1 -2 -3 -3
输出 #1复制
2
说明/提示
对于 30 \%30% 的数据,N,M \le 100N,M≤100,P,Q \le 200P,Q≤200;
对于 80 \%80% 的数据,N,M \le 4 \times 10^3N,M≤4×103,P,Q \le 10^4P,Q≤104;
对于 100 \%100% 的数据,N,M \le 10^4N,M≤104,P,Q \le 2 \times 10^4P,Q≤2×104
思路:用并查集解决。先分出两个部分存小明和小红的关系图。再分开求每个公司与明红的朋友数,因为只能一夫一妻,故输出数小的那个
#include<bits/stdc++.h> using namespace std; int fa[20001]; int N,M; int Xi,Yi; int ai,bi,ci,di; int s1=0,s2=0; int find(int x) { if(fa[x]==x)return x; return fa[x]=find(fa[x]); } void merge(int x,int y) { fa[find(x)]=find(y); } int main() { cin >> N >>M>> Xi>>Yi; for(int i=0;i<=N+M;++i){fa[i]=i;} while(Xi--){ cin >> ai >>bi; merge(ai,bi); } while(Yi--){ cin >> ci >>di; merge(ci*(-1)+N,di*(-1)+N); } int q; for(int i=1;i<=N;i++) if(find(i)==find(1)) s1++; for(int i=N+1;i<=N+M;i++) if(find(i)==find(N+1)) s2++; if(s2>s1)printf("%d",s1); else printf("%d",s2); }