#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std ; const int maxNode = 400500 ; struct tireTree { int _cnt ; int tr[ maxNode ][ 30 ] ; int cnt[ maxNode ] ; tireTree ( ) { memset ( tr , 0 , sizeof( tr ) ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; _cnt = 1 ; } void insert ( char str[ ] ) { int len = strlen( str + 1 ) , cur = 1 ; for ( int i=1 ; i<=len ; ++i ) { int idx = str[ i ] - 'a' ; if ( !tr[ cur ][ idx ] ) tr[ cur ][ idx ] = ++_cnt ; cur = tr[ cur ][ idx ] ; ++cnt[ cur ] ; } } int getPrefix ( char str[ ] ) { int len = strlen( str + 1 ) , cur = 1 ; for ( int i=1 ; i<=len ; ++i ) { int idx = str[ i ] - 'a' ; if ( !tr[ cur ][ idx ] ) return 0 ; cur = tr[ cur ][ idx ] ; } return cnt[ cur ] ; } } ; tireTree tree ; char str [ 105 ] ; int main ( ) { while ( gets ( str + 1 ) ) { if ( !strlen( str + 1 ) ) break ; tree.insert( str ) ; } while ( gets( str + 1 ) ) printf ( "%d " , tree.getPrefix( str ) ) ; return 0 ; }
#include <cstdio> #include <stack> #include <cstring> using namespace std ; const int maxN = 50010 ; int In ( ) { int x = 0 , f = 1 ; char ch = getchar ( ) ; while ( ch < '0' || ch > '9' ) { if ( ch == '-')f = -1 ; ch = getchar ( ) ;} while ( ch >= '0' && ch <= '9' ) { x = ( x << 1 ) + ( x << 3 ) + ch - '0' ; ch = getchar ( ) ;} return x * f ; } stack <int> stk ; int arr[ maxN ] , ansl[ maxN ] , ansr[ maxN ] ; int main ( ) { for ( int Kase = In ( ) , kase = 1 ; kase <= Kase ; ++kase ) { memset ( ansl , 0 , sizeof ( ansl ) ) ; memset ( ansr , 0 , sizeof ( ansr ) ) ; int N = In ( ) ; for ( int i=1 ; i<=N ; ++i ) arr[ i ] = In ( ) ; while(!stk.empty()) stk.pop() ; for ( int i=1 ; i<=N ; ++i ) { while ( !stk.empty() && arr[ stk.top() ] < arr[ i ] ) { ansl[ i ] = stk.top() ; stk.pop() ; } stk.push( i ) ; } while(!stk.empty()) stk.pop() ; for ( int i=N ; i>=1 ; --i ) { while ( !stk.empty() && arr[ stk.top() ] < arr[ i ] ) { ansr[ i ] = stk.top() ; stk.pop() ; } stk.push( i ) ; } printf ( "Case %d: " , kase ) ; for ( int i=1 ; i<=N ; ++i ) { printf ( "%d %d " , ansl[ i ] , ansr[ i ] ) ; } } return 0 ; }
#include <string.h> #include <stdio.h> #include <iostream> using namespace std ; class bigInt { private : int num[10000], size; static const int maxN = 10000; public : bigInt(char *ch) { size = strlen(ch); for(int i = 0; i < size; ++i) { if ( num[ i ] >= '0' && num[ i ] <= '9' ) num[i] = ch[size - i - 1] - '0'; } } void Plus(bigInt o) { bool up = 0; size = size > o.size ? size : o.size ; for ( int i = 0 ; i < size ; ++i ) { num[ i ] += o.num[ i ] + up ; up = false ; if ( num[ i ] >= 10 ) { num[ i ] -= 10 ; up = true ; } } if ( up ) num[ size++ ] = 1; } void print() { for ( int i = size - 1 ; i >= 0 ; --i ) { printf("%d", num[i]); } } }; char str1[10000], str2[10000]; int main() { int T ; cin >> T ; for(int k = 1; k <= T; ++k) { scanf("%s %s", &str1, &str2); bigInt A = bigInt ( str1 ) ; bigInt B = bigInt ( str2 ) ; A.Plus ( B ) ; printf("Case %d: ", k); printf("%s + %s = ", str1, str2); A.print(); printf(" "); if ( k != T ) printf(" "); } return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std ; const int maxN = 100100 ; const int INF = 2147483647 ; int In ( ) { int x = 0 , f = 1 ; char ch = getchar ( ) ; while ( ch < '0' || ch > '9' ) { if ( ch == '-')f = -1 ; ch = getchar ( ) ;} while ( ch >= '0' && ch <= '9' ) { x = ( x << 1 ) + ( x << 3 ) + ch - '0' ; ch = getchar ( ) ;} return x * f ; } int f[ maxN ] , arr[ maxN ] , sum[ maxN ] ; int main ( ) { int Kase = In ( ) ; for ( int kase=1 ; kase<=Kase ; ++kase ) { int N = In ( ) ; memset ( sum , 0 , sizeof ( sum ) ); memset ( f , 0 , sizeof ( f ) ); for ( int i=1 ; i<=N ; ++i ) { sum[ i ] = sum[ i-1 ] + ( arr[ i ] = In ( ) ) ; } for ( int i=1 ; i<=N ; ++i ) { f[ i ] = max ( f[ i - 1 ] + arr[ i ] , arr[ i ] ) ; } int ans = -INF , indexOfMax , start = 0 ; for ( int i=1 ; i<=N ; ++i ) { if ( ans < f[ i ] ) indexOfMax = i , ans = f[ i ] ; } for ( int i=indexOfMax ; i>=0 ; --i ) { if ( sum[ indexOfMax ] - sum[ i ] == ans ) start = i ; } printf ("Case %d: " , kase ) ; cout << ans << ' ' << start + 1 << ' ' << indexOfMax << endl ; if ( kase != Kase ) cout << endl ; } return 0 ; }
#include <cstring> #include <iostream> #include <cstdio> #include <map> using namespace std ; int main ( ) { int N ; while ( cin >> N && N ) { map <string,int> mapColor ; for ( int i=1 ; i<=N ; ++i ) { string str ; cin >> str ; ++ mapColor[ str ] ; } map<string,int>::iterator it ; int _cnt = 0 ; string strr ; for ( it = mapColor.begin ( ) ; it != mapColor.end ( ) ; ++it ) { if ( it -> second > _cnt ) { _cnt = it -> second ; strr = it -> first ; } } cout << strr << endl ; } return 0 ; }
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std ; const int maxN = 10010 ; struct Edge { int to , next , val ; }e[ maxN << 1 ] ; long long head[ maxN ] , s[ maxN ] ; int _cnt ; double ans ; void addEdge ( int _x , int _y , int _val ) { e[ ++_cnt ].to = _y ; e[ _cnt ].next = head[ _x ] ; head[ _x ] = _cnt ; e[ _cnt ].val = _val ; } void dfs ( const int x , const int fa , const int n ) { s[ x ] = 1 ; for ( int i=head[ x ] ; i ; i=e[i].next ) { if ( e[i].to != fa ) { dfs( e[i].to , x , n ) ; s[x] += s[e[i].to] ; ans += s[e[i].to] * ( n - s[e[i].to] ) * e[ i ].val ; } } } int main ( ) { int Kase ; scanf ("%d" ,&Kase ) ; for ( int k=1 ; k<=Kase ; ++k ) { int N ; scanf("%d",&N); memset(e,0,sizeof(e)); memset(head,0,sizeof(head)); memset(s,0,sizeof(s)); _cnt = 0; ans = 0 ; for ( int i=1 ; i<N ; ++i ) { int x , y , val ; scanf("%d%d%d" ,&x,&y,&val) ; addEdge ( ++x , ++y , val ) ; addEdge ( y , x , val ) ; } dfs( 1 , 1 , N ); printf("%.6lf ",ans*2.0/N/(N-1)); } return 0; }
#include <bits/stdc++.h> using namespace std; int n,len,a[20],b[20],cnt; int cmp(int a,int b){ return a>b; } void dfs(int x,int posa,int sum,int posb){ if(sum>n) return; if(sum == n){ cnt++; for(int i = 0; i<posb; i++) { if(i) printf("+%d",b[i]); else printf("%d",b[i]); } printf(" "); } for(int i = posa; i<len; i++){ b[posb] = a[i]; dfs(a[i],i+1,sum+a[i],posb+1); while(i+1<len && a[i] == a[i+1]) i++; } } int main(){ int i; while(~scanf("%d%d",&n,&len),n+len!=0){ for(i = 0; i<len; i++) scanf("%d",&a[i]); sort(a,a+len,cmp); printf("Sums of %d: ",n); cnt = 0; dfs(0,0,0,0); if(!cnt)printf("NONE "); } return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> using namespace std ; const int inf = 2147483647 ; int wall[55][55][55],vis[55][55][55]; int xx[]={0,1,0,0,-1,0,0}; int yy[]={0,0,1,0,0,-1,0}; int zz[]={0,0,0,1,0,0,-1}; int ans ; struct Point { int x,y,z,t; }; bool judge ( Point p ) { return wall[p.x][p.y][p.z]==0 && !vis[p.x][p.y][p.z] ; } void bfs ( int A,int B,int C,int lim ) { queue <Point> Q; Q.push(Point{1,1,1,0}); vis[1][1][1] = 1; while( !Q.empty() ) { Point tmp = Q.front(); Q.pop() ; if ( tmp.x == A && tmp.y == B && tmp.z == C ) ans = min ( ans , tmp.t ) ; for ( int i=1 ; i<=6 ; ++i ) { int tx = tmp.x + xx[ i ] ; int ty = tmp.y + yy[ i ] ; int tz = tmp.z + zz[ i ] ; if ( judge(Point{tx,ty,tz,tmp.t+1}) ) { vis[tx][ty][tz] = 1 ; Q.push(Point{tx,ty,tz,tmp.t+1}) ; } } } } int main ( ) { int T ,lim; int A , B , C ; scanf("%d",&T) ; while ( T-- ) { memset(wall,-1,sizeof(wall)); memset(vis,0,sizeof(wall)); scanf("%d %d %d %d",&A,&B,&C,&lim); for ( int i=1 ; i<=A ; ++i ) { for ( int j=1 ; j<=B ; ++j ) { for ( int k=1 ; k<=C ; ++k ) { scanf( "%d" ,&wall[i][j][k] ) ; } } } ans = inf ; bfs(A,B,C,lim); // cout << ans << endl ; cout << ( ans <= lim ? ans : -1) << endl ; } return 0 ; }
#include <cstdio> #include <cstring> #include <iostream> #include <stack> using namespace std; int main ( ) { char str[ 10000 ] ; while(scanf("%s",str+1)==1){ stack <char> S ; for( int i=1 ; str[i]!='