Section 1.1
PROB Broken Necklace |
这里也是巧解……
/* ID:liwei101 PROG:beads LANG:C++ */ #include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <list> #include <queue> #include <string> #include <cstring> #include <map> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define pri(a) printf("%d\n",a) #define MM 1000002 #define MN 2005 #define INF 168430090 using namespace std; typedef long long ll; int main() { ifstream fin("beads.in"); ofstream fout("beads.out"); int n,i,go=0,pre=0,w=0,sum=0; string s; char c=‘ ‘; fin>>n>>s; s+=s; //圆圈字符串可以把其变为两个字母串连接,首尾相连即可…… for(i=0;i<n*2;i++) { if(s[i]==‘w‘) go++,w++; else if(s[i]==c) go++,w=0; else { sum=max(go+pre,sum); //两端项链之和 pre=go-w; //左端就是右端减去有字母w的个数 go=w+1; //右端往前刚开始的个数就是前面有字母w个数加上当前的一个 w=0; //w个数重置为0 c=s[i]; //当前字母 } } sum=max(go+pre,sum); fout<<(sum>n?n:sum)<<endl; return 0; }
Section 1.2 PROB Milking Cows |
/* ID:liwei101 PROG:milk2 LANG:C++ */ #include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <list> #include <queue> #include <string> #include <cstring> #include <map> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define pri(a) printf("%d\n",a) #define MM 1000002 #define MN 2005 #define INF 168430090 using namespace std; typedef long long ll; int hash[MM],s[3]; int main() { ifstream fin("milk2.in"); ofstream fout("milk2.out"); int n,i,j,a,b,Min=MM,Max=0; fin>>n; for(i=0; i<n; i++) { fin>>a>>b; b--; Min=min(Min,a); Max=max(Max,b); for(j=a; j<=b; j++) hash[j]=1; //把出来过的标记为1 } for(i=Min; i<Max; i+=j) { for(j=0; hash[i+j]==hash[i]; j++); //这句话就是为什么刚开始得:b--了,如果不减的话,这两句循环都得再减1才行…… s[hash[i]]=max(s[hash[i]],j); //哈希取址的思想真的太强大了…… } fout<<s[1]<<‘ ‘<<s[0]<<endl; return 0; }
/* ID:liwei101 PROG:namenum LANG:C++ */ #include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <list> #include <queue> #include <string> #include <cstring> #include <map> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define pri(a) printf("%d\n",a) #define MM 1000002 #define MN 2005 #define INF 168430090 using namespace std; typedef long long ll; int main() { ifstream fin("namenum.in"); ofstream fout("namenum.out"); ifstream dict("dict.txt"); string s,ss; int a[27],flag=1; a[‘A‘-‘A‘]=a[‘B‘-‘A‘]=a[‘C‘-‘A‘]=2; a[‘D‘-‘A‘]=a[‘E‘-‘A‘]=a[‘F‘-‘A‘]=3; a[‘G‘-‘A‘]=a[‘H‘-‘A‘]=a[‘I‘-‘A‘]=4; a[‘J‘-‘A‘]=a[‘K‘-‘A‘]=a[‘L‘-‘A‘]=5; a[‘M‘-‘A‘]=a[‘N‘-‘A‘]=a[‘O‘-‘A‘]=6; a[‘P‘-‘A‘]=a[‘R‘-‘A‘]=a[‘S‘-‘A‘]=7; a[‘T‘-‘A‘]=a[‘U‘-‘A‘]=a[‘V‘-‘A‘]=8; a[‘W‘-‘A‘]=a[‘X‘-‘A‘]=a[‘Y‘-‘A‘]=9; fin>>ss; while(dict>>s) { string sss; for(int i=0;i<s.size();i++) { sss+=a[s[i]-‘A‘]+‘0‘; } if(sss==ss) {fout<<s<<endl;flag=0;} } if(flag) fout<<"NONE"<<endl; return 0; }
/* PROG:palsquare LANG:C++ ID:liwei101 */ #include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <list> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define pri(a) printf("%d\n",a) #define f(i,a,n) for(i=a;i<n;i++) #define F(i,a,n) for(i=a;i<=n;i++) #define MM 1000002 #define MN 5005 #define INF 1000000007 using namespace std; typedef long long ll; char fuck[22]={‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘}; string convert(int i,int b) { string a; while(i) { a+=fuck[i%b]; i/=b; } for(int i=0,j=a.size()-1;i<j;i++,j--) { char c=a[i]; a[i]=a[j]; a[j]=c; } return a; } int cmp(string s) { int i,j; for(i=0,j=s.size()-1;i<j;i++,j--) if(s[i]!=s[j]) return 0; return 1; } int main() { ofstream fout("palsquare.out"); ifstream fin("palsquare.in"); int i,b; fin>>b; for(i=1;i<=300;i++) { string s1=convert(i,b); string s2=convert(i*i,b); if(cmp(s2)) fout<<s1<<‘ ‘<<s2<<endl; } return 0; }
服务器编程入门(7)I/O复用,布布扣,bubuko.com
原文:http://blog.csdn.net/zs634134578/article/details/19929449