首页 > 代码库 > USACO 3.1 Contact

USACO 3.1 Contact

http://www.nocow.cn/index.php/Translate:USACO/contact

题目大意:给一个只含0和1的序列,统计每个子序列的重复次数,并按次数递减来输出

考虑子序列时将序列前面加一个‘1‘然后转化成二进制整数,这样每个子序列与整数一一对应,统计二进制整数出现次数,按要求输出即可(ps:usaco老是喜欢在输出上做文章)代码如下(未提交):

  1 /**********************************************  2 ***    Problem:  3 ***    Author:        JKL  4 ***    University:    CSUST  5 ***    Team:          __Dream  6 ***    Email:          1451108308@QQ.COM  7 ***    My Blog:        http://www.cnblogs.com/jklongint/  8 ***********************************************/  9 //=================================================== 10 #include <iostream> 11 #include <fstream> 12 #include <sstream> 13 #include <iomanip> 14 #include <cstdio> 15 #include <cstdlib> 16 #include <cmath> 17 #include <cassert> 18 #include <numeric> 19 #include <ctime> 20 #include <algorithm> 21 #include <cstring> 22 #include <string> 23 #include <vector> 24 #include <queue> 25 #include <map> 26 #include <stack> 27 #include <list> 28 #include <set> 29 #include <bitset> 30 #include <deque> 31 using namespace std; 32 //--------------------------------------------------- 33 #define mem(a,b) memset(a,b,sizeof(a)) 34 #define GO cout<<"HelloWorld!"<<endl 35 #define Case(x) cout<<"Case "<<x<<":" 36 #define foru(i,n) for(int i=1; i <= n; i++) 37 #define ford(i,n) for(int i = n; i >= 1; i--) 38  #define fin freopen("input.txt","r",stdin); 39  #define fout freopen("output.txt","w",stdout) 40 #define lson  l, m, rt << 1 41 #define rson  m + 1, r, rt << 1 | 1 42  43 #define sqr(a)  ((a)*(a)) 44 #define abs(a) ((a>0)?(a):-(a)) 45 #define pii pair<int,int> 46  47 #define fmax(a,b) max(a,b) 48 #define fmin(a,b) min(a,b) 49 #define fmax3(a,b,c)  (fmax(a,fmax(a,b))) 50 #define fmin3(a,b,c)  (fmin(a,fmin(a,b))) 51  52 #define sfi(x) scanf("%d",&x) 53 #define sfL(x) scanf("%I64d",&x) 54 #define sfc(x) scanf("%c",&x) 55 #define sfd(x) scanf("%lf",&x) 56 #define sfs(x) scanf("%s",x) 57 #define sfii(a,b) scanf("%d%d",&a,&b) 58 #define sfLL(a,b) scanf("%I64d%I64d",&a,&b) 59 #define sfcc(a,b) scanf("%c%c",&a,&b) 60 #define sfdd(a,b) scanf("%lf%lf",&a,&b) 61 #define sfss(a,b) scanf("%s%s",a,b) 62  63 #define pfi(x) printf("%d",x) 64 #define pfL(x) printf("%I64d",x) 65 #define pfs(x) printf("%s",x) 66 #define pfd(x) printf("%lf",x) 67 #define pfc(x) print("%c",x) 68 #define newLine pfs("\n") 69 #define space pfs(" ") 70  71 //-------------------------------------------------------- 72 typedef __int64 LL; 73 typedef unsigned long long ULL; 74 //typedef __int64 __LL; 75 typedef unsigned __int64 __ULL; 76  77 typedef vector<int> vi; 78 typedef vector<LL> vL; 79 typedef vector<string> vs; 80 typedef set<int> si; 81 typedef map<int,int> mii; 82 typedef map<LL,LL> mLL; 83 typedef map<string,int> msi; 84 typedef map<char,int> mci; 85 //-------------------------------------------------------- 86 const int dx[4]={1,-1,0,0}; 87 const int dy[4]={0,0,1,-1}; 88  const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 89  const int N6=1000006; 90  const int N5=100006; 91  const int N4=10006; 92  const int N3=1006; 93  const int N2=106; 94  const int N=200009; 95  const int MOD=1000000007; 96  const LL LMAX=0x7fffffffffffffff; 97  const LL IMAX=0x3fffffff; 98  const double PI=3.14159265359; 99 //--------------------------------------------------------100 template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }101 template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }102 103 //------------------------------------------------------------104 struct TreeNode{105     LL  sum;106 };107 struct Node{108     int id, s;109 };110 char sbuf[20];111 //=================================================================112 Node node[N4];113 int cnt[N4],  f[N4];114 char s[N];115 int getState(int n, int len)116 {117     int c = 1;118     foru(i, len) c = c * 2 + s[n + i- 1] - 48;119     return c;120 }121 char *getString(int a)122 {123     int c = 0;124     mem(sbuf, 0);125     while(a > 0){126         sbuf[c++] = (a & 1) + 48;127         a = a >> 1;128     }129     c--;130     sbuf[c] = 0;131     int cc = c / 2, l = 0;132     c--;133     foru(i, cc){134         swap(sbuf[l++], sbuf[c--]);135     }136     return sbuf;137 }138 int cmp(Node i, Node j)139 {140     return i.s > j.s || i.s == j.s && i.id < j.id;141 }142 int main()143 {144     //fin;145     //fout;146     //freopen("contact.in","r",stdin);147     //freopen("contact.out","w",stdout)148     int a, b, n;149     cin>> a>> b>> n;150     sfs(s);151     int len = strlen(s);152     for(int i = len - 1; i >= 0; i--){153         for(int j = a; j <= b; j++){154             if(i + j > len)break;155             int t = getState(i, j);156             f[t] ++;157         }158     }159     int m = (1 << (b + 1)) - 1, q = b + 1;160     foru(i, m){161         node[i].s = f[i];162         node[i].id = i;163     }164     sort(node + 1, node + 1 + m, cmp);165     //foru(i, m)cout<<node[i].s <<endl;166     int cnt = 0;167     foru(i, m){168         if(node[i].s != node[i-1].s || i == 1){169             cnt++;170             if(cnt > n)break;171             if(i > 1)cout<<endl;172             cout<<node[i].s<<endl<<getString(node[i].id);173         }174         else cout<<" "<<getString(node[i].id);175     }176     return 0;177 }
View Code