Skip to content

Commit

Permalink
update 2 problems
Browse files Browse the repository at this point in the history
  • Loading branch information
pnck committed Dec 8, 2015
1 parent 7ea338a commit c099a31
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 0 deletions.
6 changes: 6 additions & 0 deletions misc-shortbin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# misc-shortbin(shortcode)
### 描述:
* GeruzoniAnsasu这么长id的人实际上是个非常懒的programer...

读入发过来的消息,存成文件然后尝试执行,根据回答决定下一步
> http://drops.hduisa.cn/archives/161/
108 changes: 108 additions & 0 deletions misc-shortbin/misc-shortbin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/python
#coding:utf-8

import SocketServer
#from zio import *
import string
import os
from hashlib import md5
import subprocess

class MyServer(SocketServer.ThreadingTCPServer,object):
allow_reuse_address = True

class MyHandler(SocketServer.StreamRequestHandler):
def handle(self):
print 'get connection from',self.client_address
self.request.settimeout(10)
self.wfile.write('TOKEN=')
self.token = self.rfile.readline().strip()
print 'TOKEN='+self.token
if '' == self.token:
self.wfile.write('\n\nwrong token\n')
return
print 'serving team {'+self.token+'} from ',self.client_address

self.wfile.write('\nProgrammers are alwas trying to write shorter code, \
so that they can finish their work earlier and enjoy a cup of java.\n')
self.rfile.readline()
self.wfile.write('Since I am a very lazy programmer, a typical "Hello world" program wrote by me usually has only 56 bytes.\n')
self.rfile.readline()
self.wfile.write("But I think that's enough. There has been a lot of things can be done, within 60 bytes.\n")
self.rfile.readline()
self.wfile.write('We are all programmers. Do you agree with me?\n')
rpl = self.rfile.readline().strip()
if rpl.lower().find('no') >= 0:
self.wfile.write('Ah... what a pitty:(\n')
return
elif rpl.lower().find('yes') >= 0:
self.wfile.write("Hmm.. I'm happy to hear that:) But I must test your honesty.\n")
self.rfile.readline()
self.wfile.write("I will ask you some questions, please answer them in our PROGRAMmer WAY:)\n")
self.wfile.write('Remember that famous word? Talk is cheap, show me the CODE :)\n\n')
self.wfile.write('***Hey your codes never compiled? <<<-I AM THE LAST HINT.\n\n')

QnA = (('Q1:Do you enjoy tea or coffee?','coffee'),('Q2:Do you like hacking things?','yes'),('Q3:Will you hack me?','no'))
rply = {\
'coffee':\
("Haha nice:)\n",\
"Hey don't you like java? There is nothing worth talking with you:( BYE\n"),\
'yes':\
('Great! We are the same:)\n',\
"Are you really a hacker? Why do you come here?\n"),\
'no':\
(':) Alright, give you the flag:',
'FxxK YOU!\n')}
LEN_LIMIT = 60
for i,t in enumerate(QnA):
q,a = t
self.wfile.write(q+'\n')
d = self.request.recv(200)
print '-'*30+'\n{}\n'.format(repr(d))+'-'*30+'\n'*2
if len(d) > LEN_LIMIT:
self.wfile.write('Wow too long!\n')
return
if d.find('ELF')<0 or d.find('main')>=0 :
self.wfile.write('Huh..?Are your sure you understood me?\n')
return
h = md5(d).hexdigest();
print h
with open('/tmp/'+h,'wb') as f:
f.write(d)
os.system('chmod 777 /tmp/'+h)
#io = zio('/tmp/'+h,timeout = 0.1)
pp = subprocess.Popen('/tmp/'+h,stdout=subprocess.PIPE)
pp.wait()
ans = pp.stdout.read().strip('\x00 \n')
pp.stdout.close()
#ans = ''
#try:
# ans = io.read(100).strip('\0 \n\t')[:len(a)]
#except EOF:
# pass
#except TIMEOUT:
# pass
os.system('pkill -9 '+h)
os.system('rm -f /tmp/'+h)
print '**{}----------{}**'.format(str(list(a)),str(list(ans)))
print cmp(ans ,a.strip())
if ans != a.strip():
self.wfile.write(rply[a][1])
break
else:
if i < 2:
self.wfile.write(rply[a][0]+"\n...Oh lazy me... This time can you give me shorter answer?\n\n")
LEN_LIMIT -= 4
else:
self.wfile.write('here you got the flag, but i removed some code from original file.\n')
print 'team {%s} got the flag.' % self.token
else:
self.wfile.write(':( You are not listening.\n')



if __name__ == "__main__":
host = '0.0.0.0'
port = 9999
s = MyServer((host, port), MyHandler)
s.serve_forever()
Binary file added misc-shortbin/test0
Binary file not shown.
Binary file added misc-shortbin/test1
Binary file not shown.
59 changes: 59 additions & 0 deletions ppc-redef-calc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## Description:

* GeruzoniAnsasu的舍友M大大最近去旁听了编译原理,回来问了一个问题把大家都难住了:为啥处理语法树的时候要有人为规定的优先级?如果优先级重新定义的话……

### 原题文档:
```
Lzy有n个数字a1,a2....an,n-1个符号(+,-,*,/)
他问***假如运算没了优先级,各种运算顺序下的结果总和对1e9+7取模的结果是多少?
***笑了笑。
ex:1*2+3
(1*2)+3 = 5
1*(2+3)= 5
结果是10.
输入数据:第一行n表示共有n个数字,随后n个数字,最后符号串。数据范围2<=n<=100,(0≤ai≤109)
输出数据: 结果
sample input:
3
1 1 1
++
sample output:
6
```
--------------
加多点数据:)

## 原出题acmer的writeup
```
这是一道简单区间DP问题。
先来分析复杂度O(n^3),第一层处理数字的个数i,第二层从第几个开始t,第三层以哪个符号为最后结束。
再分类讨论,+,-为一类,*为一类。
第一类:左边的结果*右边符号数的阶乘 + 右边的结果*左边符号数的阶乘
第二类:左边结果*右边结果即可
注意还要乘以组合数,即为答案。
讲得好抽象,不过很多队伍做出来,这里就简要描述。
主要代码部分,c为组合数,f为阶乘数,注意取模
for(int i = 2; i <= n; i++)
{
for(int t = 1; t + i -1 <= n; t++)
{
for(int d = 1; d <=i-1; d++)
{
int w = t+d-1;
char ch = s[w];
if(ch=='+')
dp[i][t] = (dp[i][t] + (c[i-2][d-1]*(dp[d][t]*f[i-1-d]%mod + dp[i-d][t+d]*f[d-1]%mod)%mod)%mod)%mod;
else if(ch=='-')
dp[i][t] = (dp[i][t] + (c[i-2][d-1]*(dp[d][t]*f[i-1-d]%mod - dp[i-d][t+d]*f[d-1]%mod)%mod)%mod)%mod;
else
dp[i][t] = (dp[i][t] + (c[i-2][d-1]*(dp[d][t]*dp[i-d][t+d]%mod)%mod)%mod)%mod;
}
}
}
By ——Lzy
```
Binary file added ppc-redef-calc/calc
Binary file not shown.
77 changes: 77 additions & 0 deletions ppc-redef-calc/sample-code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
#define inf 0x3f3f3f3f
#define LL long long
#define clr(a,b) memset(a,b,sizeof(a))

using namespace std;

int a[1055];
char s[1055];
LL c[1055][1055];
LL dp[1055][1055];
LL f[1055];
const int mod = 1e9 + 7;

void init()
{
c[0][0] = 1;
for(int i =1; i <= 1050; i++)
{
c[i][0] = c[i][i] = 1;
for(int j = 1; j < i; j++)
c[i][j] = (c[i-1][j] + c[i-1][j-1])%mod;
}
f[0] = f[1] = 1;
for(int i =2; i <= 1050; i++)
f[i] = i*f[i-1]%mod;
}

int main(int argc,char *argv[])
{
int n;
init();
if(scanf("%d",&n) || 1 )// patched from while
{
for(int i =1; i <= n; i++)
scanf("%d",a+i);
scanf("%s",s+1);
clr(dp,0);
for(int i = 1; i <= n; i++)
dp[1][i] = a[i];
for(int i = 2; i <= n; i++)
{
for(int t = 1; t + i -1 <= n; t++)
{
for(int d = 1; d <=i-1; d++)
{
int w = t+d-1;
char ch = s[w];
if(ch=='+')
dp[i][t] = (dp[i][t] + (c[i-2][d-1]*(dp[d][t]*f[i-1-d]%mod + dp[i-d][t+d]*f[d-1]%mod)%mod)%mod)%mod;
else if(ch=='-')
dp[i][t] = (dp[i][t] + (c[i-2][d-1]*(dp[d][t]*f[i-1-d]%mod - dp[i-d][t+d]*f[d-1]%mod)%mod)%mod)%mod;
else
dp[i][t] = (dp[i][t] + (c[i-2][d-1]*(dp[d][t]*dp[i-d][t+d]%mod)%mod)%mod)%mod;
}
}
}
LL ans = dp[n][1];
if(ans < 0 ) ans += mod;
printf("%lld\n",ans);
}
}
Loading

0 comments on commit c099a31

Please sign in to comment.