比赛地址:
http://codeforces.com/contest/1025
A题:
题意:给你一串字符串,要求最后把字符串通过一定数量的操作后,变成同一个字母,操作是选择一个字母x(这个字母必须在字符串中出现不止一次),然后把字符串中所有的x变为y(任意字母),这个操作可以进行无限次。问你能否完成要求,能就输出"Yes",否则输出"No"。
思路:读懂题意之后,很容易想到,只要字符串中有一个字母x(在字符串中出现不止一次),那么就可以完成要求,于是很快就撸完代码,过了样例,就交了,结果wa了,瞬间懵了,分析了一下,发现我的方法并没有问题,于是几乎没怎么改,抱着侥幸心理,又交了一发,结果还是wa,然后根据我之前一篇的debug总结,首先检查了输出格式,发现没有区分大小写,于是改了又交了一发,结果还是wa,之后猜想可能是有极限情况没有考虑,看了看提交状况,发现很多人和我一样wa在第四个样例,那么几乎可以确定,第四个样例是一个特殊样例了。然后顺着debug总结,开始想极限情况,才发现当字符串只有一个字母这种特例。改了之后就a了。
总结:前面的三次wa完全是可以避免的,只要我顺着debug总结,一次性把各个步骤检查完毕,就可以a了。这也提醒自己,结果是wa的时候,可能存在不止一处的错误,一定要尽力改全!
代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define Fin freopen("in.txt","r",stdin)
#define Fout freopen("out.txt","w",stdout)
#define Case(T) int T;for(scanf("%d",&T);T--;)
#define fo(i,a,b) for(int i = a; i < b; ++i)
#define fd(i,a,b) for(int i = a; i >= b; --i)
#define me(a,b) memset(a,b,sizeof(a))
#define fi(a,n,val) fill(a,a+n,val)
#define Scand(n) scanf("%d",&n)
#define Scans(s) scanf("%s",s)
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b ? gcd(b,a%b): a; }
const int maxn = 1e6 + 5;
const int INF = 0xffffff;
#ifndef ONLINE_JUDGE
#endif // ONLINE_JUDGE
int n;
char s[maxn];
int vis[100];
int main()
{
#ifndef ONLINE_JUDGE
//Fin;
#endif // ONLINE_JUDGE
cin >> n;
Scans(s);
if(n == 1){
cout << "YES" << endl;
return 0;
}
fo(i, 0, n){
vis[s[i]-'a']++;
if(vis[s[i]-'a'] >= 2){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
B题:
题意:给你n个对,每个对,有两个数,让你找出一个数,它是每个对的公约数,这里的公约数被重新定义了,若a是(x,y)这一对的约数,只要a是x,y中的任一个的约数就可以了。
思路:由于题目叫Weakened Common Divisor,联想到两个数的公约数,但这里是一些对的公约数,心里想,能否把一个对转化为一个数,把这两个数相乘试试,由于是猜想的,也没去尝试,看了别人的题解,发现这个思路是可行的,直接贴代码吧。
代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define Fin freopen("in.txt","r",stdin)
#define Fout freopen("out.txt","w",stdout)
#define Case(T) int T;for(scanf("%d",&T);T--;)
#define fo(i,a,b) for(int i = a; i < b; ++i)
#define fd(i,a,b) for(int i = a; i >= b; --i)
#define me(a,b) memset(a,b,sizeof(a))
#define fi(a,n,val) fill(a,a+n,val)
#define Scand(n) scanf("%d",&n)
#define Scans(s) scanf("%s",s)
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b ? gcd(b,a%b): a; }
const int maxn = 110000 + 5;
const int INF = 0xffffff;
#ifndef ONLINE_JUDGE
#endif // ONLINE_JUDGE
int n;
ll a,b;
int main()
{
// ios::sync_with_stdio(false);cin.tie(0);
#ifndef ONLINE_JUDGE
//Fin;
#endif // ONLINE_JUDGE
cin >> n;
cin >> a >> b;
fo(i, 1, n){
ll c,d;
cin >> c >> d;
ll e = c*d;
a = gcd(a, e);
b = gcd(b, e);
}
// printf("a:%lld b:%lld\n",a,b);
if(a == 1 && b == 1)
cout << -1 << endl;
else{
for(ll i=2; i*i <= a || i*i <= b; ++i)
if(a%i == 0 || b%i == 0){
cout<<i<<endl;
return 0;
}
if(a != 1)
cout << a << endl;
else
cout << b << endl;
}
return 0;
}
C题:
题意:
给你一串字符串,问你经过多次操作后,最长的”斑马线"长度是多少,所谓”斑马线"就是一串01串,即题目中bwbw或wbwbw。可以进行的操作是,在字符串中找出一个分割点,然后把两侧的字符串反转,例如 bwwwbww|bw →wwbwwwb|wb→ w|wbwwwbwb →w|bwbwwwbw→ wbwbwwwbw 这个例子答案是5
思路:由于涉及在一个串中找到一个分割点,以及反转,联想到”环“,发现只要在环中找出最长的01串就行,于是可以在把原字符串s,加倍一下,变成s+s,就可以模拟一个环了。
由于涉及转化,所以得注意,转换后得到的答案,是否超出原串的答案范围。本题就有一个wa点,就是最终答案不会超过原串的长度。
代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define Fin freopen("in.txt","r",stdin)
#define Fout freopen("out.txt","w",stdout)
#define Case(T) int T;for(scanf("%d",&T);T--;)
#define fo(i,a,b) for(int i = a; i < b; ++i)
#define fd(i,a,b) for(int i = a; i >= b; --i)
#define me(a,b) memset(a,b,sizeof(a))
#define fi(a,n,val) fill(a,a+n,val)
#define Scand(n) scanf("%d",&n)
#define Scans(s) scanf("%s",s)
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b ? gcd(b,a%b): a; }
const int maxn = 1e6 + 50;
const int INF = 0xffffff;
#ifndef ONLINE_JUDGE
#endif // ONLINE_JUDGE
//char s[maxn];
int main()
{
// ios::sync_with_stdio(false);cin.tie(0);
#ifndef ONLINE_JUDGE
//Fin;
#endif // ONLINE_JUDGE
// Scans(s);
// char *ss = strcat(s, s);
// int len1 = strlen(s);
// int len2 = strlen(ss);
string s,ss;
cin >> s;
int len1 = s.length();
ss = s+s;
int len2 = ss.length();
ll cnt = 1;
ll ans = -1;
fo(i, 1, len2){
if(ss[i] != ss[i-1])
cnt++;
else{
ans = cnt > ans ? cnt : ans;
cnt = 1;
}
}
ans = cnt > ans ? cnt : ans;
// printf("ans:%lld\n",ans);
ans = len1 < ans ? len1 : ans;
cout << ans << endl;
return 0;
}
最后:没有人生而垫底,也没有天生就菜!