1. 跳一跳
#include <iostream>
using namespace std;
int main ( ) {
int cur, ans = 0 , plus = 0 ;
while ( scanf ( "%d" , & cur) != EOF && cur) {
ans + = cur;
if ( cur == 1 ) {
plus = 0 ;
} else if ( cur == 2 ) {
ans + = plus;
plus + = 2 ;
}
}
printf ( "%d\n" , ans) ;
return 0 ;
}
1 1 2 2 2 1 1 2 2 0
22
2. 碰撞的小球
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 102 ;
struct node{
int pos, id, dir = 1 ;
} ;
vector< node> v ( MAXN) ;
int cmp1 ( const node & a, const node & b) { return a. pos < b. pos; }
int cmp2 ( const node & a, const node & b) { return a. id < b. id; }
int main ( )
{
int n, l, t;
scanf ( "%d%d%d" , & n, & l, & t) ;
v. resize ( n+ 2 ) ;
v[ 0 ] . pos = 0 ;
v[ n+ 1 ] . pos = l;
for ( int i = 1 ; i <= n; ++ i) {
scanf ( "%d" , & v[ i] . pos) ;
v[ i] . id = i;
}
sort ( v. begin ( ) + 1 , v. end ( ) - 1 , cmp1) ;
for ( int i = 1 ; i <= t; ++ i) {
for ( int j = 1 ; j <= n; ++ j)
v[ j] . pos + = v[ j] . dir;
for ( int k = 1 ; k < n+ 2 ; ++ k) {
if ( v[ k- 1 ] . pos == v[ k] . pos) {
v[ k- 1 ] . dir = - v[ k- 1 ] . dir;
v[ k] . dir = - v[ k] . dir;
}
}
}
sort ( v. begin ( ) + 1 , v. end ( ) - 1 , cmp2) ;
for ( int i = 1 ; i <= n; ++ i)
printf ( "%d " , v[ i] . pos) ;
return 0 ;
}
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int N = 100 ;
int st[ N] ;
int ed[ N] ;
int tmp[ N] ;
int main ( )
{
map< int , int > mp;
int n, L, t;
cin >> n >> L >> t;
for ( int i = 0 ; i < n; ++ i) {
int k;
cin >> st[ i] ;
tmp[ i] = st[ i] ;
k = st[ i] + t;
k = k % ( 2 * L) ;
if ( k > L) ed[ i] = L - k% L;
else ed[ i] = k;
}
sort ( tmp, tmp + n) ;
sort ( ed, ed + n) ;
for ( int i = 0 ; i < n; i++ )
mp[ tmp[ i] ] = i;
for ( int i = 0 ; i < n; i++ )
cout << ed[ mp[ st[ i] ] ] << " " ;
return 0 ;
}
10 22 30
14 12 16 6 10 2 8 20 18 4
6 6 8 2 4 0 4 12 10 2
3. **** URL映射
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
class rule {
public :
string name;
vector< string> key;
rule ( string a, vector< string> b) : name ( a) , key ( b) { }
} ;
vector< rule> rules;
bool isMatch;
int s_to_i ( string s) {
int ans;
stringstream ss ( s) ;
ss >> ans;
return ans;
}
string i_to_s ( int a) {
stringstream ss;
ss << a;
return ss. str ( ) ;
}
bool judgeNum ( string s) {
for ( int i = 0 ; i< s. length ( ) ; i++ ) {
if ( s[ i] > '9' || s[ i] < '0' )
return false ;
}
return true ;
}
void split ( string s, string n) {
vector< string> co;
bool f = true ;
if ( s[ s. length ( ) - 1 ] != '/' ) {
f = false ;
s = s + "/" ;
}
size_t spi = s. find ( "/" ) ;
while ( spi != - 1 ) {
string str= s. substr ( 0 , spi) ;
s = s. substr ( spi + 1 , s. length ( ) ) ;
co. push_back ( str) ;
spi = s. find ( "/" ) ;
}
if ( f)
co. push_back ( "/" ) ;
rules. push_back ( rule ( n, co) ) ;
}
vector< string> match ( int k, string u) {
size_t i, j, pos = 0 ;
vector< string> result, query;
bool f = true ;
if ( u[ u. length ( ) - 1 ] != '/' ) {
f = false ;
u = u + "/" ;
}
size_t spi = u. find ( "/" ) ;
while ( spi != - 1 ) {
string s = u. substr ( 0 , spi) ;
u = u. substr ( spi + 1 , u. length ( ) ) ;
query. push_back ( s) ;
spi = u. find ( "/" ) ;
}
if ( f) query. push_back ( "/" ) ;
for ( i = 0 ; i < rules[ k] . key. size ( ) && pos < query. size ( ) ; ++ i) {
if ( rules[ k] . key[ i] == "<path>" && query[ pos] . length ( ) != 0 ) {
string s = query[ pos] ;
for ( j = pos + 1 ; j < query. size ( ) ; ++ j) {
if ( query[ j] == "/" ) s = s + "/" ;
else s = s + "/" + query[ j] ;
}
result. push_back ( s) ;
pos = query. size ( ) ;
i = rules[ k] . key. size ( ) ;
}
else if ( rules[ k] . key[ i] == "<int>" && query[ pos] . length ( ) != 0 ) {
if ( judgeNum ( query[ pos] ) ) {
result. push_back ( i_to_s ( s_to_i ( query[ pos] ) ) ) ;
pos++ ;
} else {
isMatch = false ;
break ;
}
}
else if ( rules[ k] . key[ i] == "<str>" && query[ pos] . length ( ) != 0 ) {
result. push_back ( query[ pos] ) ;
pos++ ;
}
else {
if ( rules[ k] . key[ i] == query[ pos] )
pos++ ;
else {
isMatch = false ;
break ;
}
}
}
if ( pos != query. size ( ) || i < rules[ k] . key. size ( ) )
isMatch = false ;
return result;
}
int main ( )
{
string rule, name, url;
int n, m;
cin >> n >> m;
for ( int i = 0 ; i < n; ++ i) {
cin >> rule >> name;
rule = rule. substr ( 1 , rule. length ( ) ) ;
split ( rule, name) ;
}
for ( int i = 0 ; i < m; i++ ) {
int k;
cin >> url;
url = url. substr ( 1 , url. length ( ) ) ;
for ( k = 0 ; k < n; ++ k) {
isMatch = true ;
vector< string> result = match ( k, url) ;
if ( isMatch) {
cout << rules[ k] . name;
for ( int j = 0 ; j < result. size ( ) ; j++ )
cout << " " << result[ j] ;
cout << endl;
break ;
}
}
if ( k == n) cout << "404" << endl;
}
return 0 ;
}
#include <iostream>
#include <ctype.h>
using namespace std;
const int N = 100 ;
string rUrl[ N] , nUrl[ N] ;
bool match ( string& adr, string& rule, bool flag)
{
int rlen = ( int ) rule. size ( ) ;
int alen = ( int ) adr. size ( ) ;
int apos = 0 , rpos= 0 ;
while ( apos < alen && rpos < rlen) {
if ( rule[ rpos] == adr[ apos] ) {
apos++ ;
rpos++ ;
} else {
if ( rule[ rpos++ ] != '<' ) return false ;
if ( flag) cout << ' ' ;
if ( rule[ rpos] == 'i' ) {
bool ok = false ;
while ( adr[ apos] && isdigit ( adr[ apos] ) ) {
if ( adr[ apos] != '0' )
ok = true ;
if ( flag && ok)
cout << adr[ apos] ;
apos++ ;
}
if ( ! ok) return false ;
rpos + = 4 ;
}
else if ( rule[ rpos] == 's' ) {
bool ok = false ;
while ( adr[ apos] && adr[ apos] != '/' ) {
ok = true ;
if ( flag)
cout << adr[ apos] ;
apos++ ;
}
if ( ! ok)
return false ;
rpos + = 4 ;
}
else if ( rule[ rpos] == 'p' ) {
if ( flag) {
while ( adr[ apos] )
cout << adr[ apos++ ] ;
}
return true ;
}
}
}
return rpos == rlen && apos == alen;
}
int main ( )
{
int n, m;
cin >> n >> m;
for ( int i = 0 ; i < n; i++ )
cin >> rUrl[ i] >> nUrl[ i] ;
for ( int i = 0 ; i < m; i++ ) {
string aUrl;
bool flag = true ;
cin >> aUrl;
for ( int j = 0 ; flag && j < n; j++ ) {
if ( match ( aUrl, rUrl[ j] , false ) ) {
flag = false ;
cout << nUrl[ j] ;
match ( aUrl, rUrl[ j] , true ) ;
}
}
if ( flag) cout << "404" ;
cout << endl;
}
return 0 ;
}
5 4
/articles/2003/ special_case_2003
/articles/<int>/ year_archive
/articles/<int>/<int>/ month_archive
/articles/<int>/<int>/<str>/ article_detail
/static/<path> static_serve
/articles/2004/
/articles/1985/09/aloha/
/articles/hello/
/static/js/jquery.js
year_archive 2004
article_detail 1985 9 aloha
404
static_serve js/jquery.js
4. 棋局评估
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
int mp[ 4 ] [ 4 ] ;
bool hok ( int h, int f) {
return mp[ h] [ 0 ] == f&& mp[ h] [ 0 ] == mp[ h] [ 1 ] && mp[ h] [ 1 ] == mp[ h] [ 2 ] ;
}
bool lok ( int l, int f) {
return mp[ 0 ] [ l] == f&& mp[ 0 ] [ l] == mp[ 1 ] [ l] && mp[ 1 ] [ l] == mp[ 2 ] [ l] ;
}
int spa ( ) {
int res= 0 ;
for ( int i= 0 ; i< 3 ; i++ )
for ( int j= 0 ; j< 3 ; j++ )
if ( ! mp[ i] [ j] ) res++ ;
return res;
}
int win ( int f) {
int wi= 0 , ans= 1 ;
if ( hok ( 0 , f) || hok ( 1 , f) || hok ( 2 , f) ) wi= 1 ;
if ( lok ( 0 , f) || lok ( 1 , f) || lok ( 2 , f) ) wi= 1 ;
if ( mp[ 0 ] [ 0 ] == f&& mp[ 0 ] [ 0 ] == mp[ 1 ] [ 1 ] && mp[ 1 ] [ 1 ] == mp[ 2 ] [ 2 ] ) wi= 1 ;
if ( mp[ 0 ] [ 2 ] == f&& mp[ 0 ] [ 2 ] == mp[ 1 ] [ 1 ] && mp[ 1 ] [ 1 ] == mp[ 2 ] [ 0 ] ) wi= 1 ;
if ( ! wi) return 0 ;
ans+ = spa ( ) ;
return ( f== 1 ) ? ans: - ans;
}
int dfs ( int peo) {
if ( ! spa ( ) ) return 0 ;
int Max= - 10 , Min= 10 ;
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 , w; j< 3 ; j++ ) {
if ( ! mp[ i] [ j] ) {
mp[ i] [ j] = peo+ 1 ;
w= win ( peo+ 1 ) ;
if ( w) {
mp[ i] [ j] = 0 ;
return w> 0 ? max ( Max, w) : min ( Min, w) ;
}
if ( ! peo) Max= max ( Max, dfs ( 1 ) ) ;
else Min= min ( Min, dfs ( 0 ) ) ;
mp[ i] [ j] = 0 ;
}
}
}
return peo? Min: Max;
}
int main ( ) {
#ifdef Mytest
freopen ( "in.txt" , "r" , stdin ) ;
#endif
int t;
cin>> t;
while ( t-- ) {
for ( int i= 0 ; i< 3 ; i++ )
for ( int j= 0 ; j< 3 ; j++ )
cin>> mp[ i] [ j] ;
int x= win ( 1 ) , y= win ( 2 ) ;
if ( x) { cout<< x<< endl; continue ; }
if ( y) { cout<< y<< endl; continue ; }
cout<< dfs ( 0 ) << endl;
}
return 0 ;
}
3
1 2 1
2 1 2
0 0 0
2 1 1
0 2 1
0 0 2
0 0 0
0 0 0
0 0 0
3
-4
0
5. 二次求和