CSU 2034: Column Addition 2035: Cafe Bazaar 2036: Getting Back Home 2037: Mars
#include <cstdio>
#include<map>
#include<algorithm>
#include<cstring>
#include<iostream>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<int, LL>PIL;
typedef pair<LL, int> PLI;
typedef pair<LL, LL> PLL;
const int MX = 1e2 + 5;
const int MXM = MX * 40;
vector<PLI>ver;
struct Trie {
int nxt[MXM][2], cnt[MXM];
int root, rear;
int new_node() {
cnt[++rear] = 0;
memset (nxt[rear], 0 , sizeof (nxt[rear]) );
return rear;
}
void init () {
rear = 0;
root = new_node();
}
void insert (LL x) {
int now = root, tmp;
for (int i = 40; i; i--) {
tmp = x >> (i - 1) & 1;
if (!nxt[now][tmp]) nxt[now][tmp] = new_node();
now = nxt[now][tmp];
cnt[now]++;
}
}
void query() {
dfs (root, 0, 0, -1);
}
void add (LL x, int y) {
for (int i = y + 1; i <= 40; i++) x <<= 1;
ver.push_back (PLI (x, y) );
}
bool check1 (int now, int d) {
int now1 = now, now2 = now;
for (int i = d; i <= 40; i++) {
if (nxt[now1][0] == 0 || nxt[now2][1] == 0) return 0;
now1 = nxt[now1][0];
now2 = nxt[now2][1];
}
return 1;
}
bool check2 (int now, int d, int s) {
for (int i = d; i <= 40; i++) {
if (nxt[now][s] == 0) return 0;
now = nxt[now][s];
}
return 1;
}
void dfs (int now, int d, LL val, int s) {
if (d == 40) {
add (val, d);
return;
}
if (s == -1) {
if (check1 (now, d + 1) ) {
add (val, d);
return;
}
if (nxt[now][0] && nxt[now][1]) {
dfs (nxt[now][0], d + 1, val << 1, 0);
dfs (nxt[now][1], d + 1, val << 1 | 1, 1);
} else if (nxt[now][0]) dfs (nxt[now][0], d + 1, val << 1, -1);
else dfs (nxt[now][1], d + 1, val << 1 | 1, -1);
} else {
if (check2 (now, d + 1, s) ) {
add (val, d);
return;
}
if (nxt[now][s ^ 1] == 0) add ( val << 1 | (s ^ 1), d + 1), dfs (nxt[now][s], d + 1, val << 1 | s, s);
else dfs (nxt[now][s ^ 1], d + 1, val << 1 | (s ^ 1), s);
}
}
} t;
int a[MX], n;
char str[MX];
LL code (int l, int r) {
LL ret = 0, tmp = 0;
for (int i = l; i <= r; i++) {
if (str[i] == '.' || i == r) {
ret = ret * 256 + tmp, tmp = 0;
continue;
}
tmp = tmp * 10 + str[i] - '0';
}
return ret;
}
bool cmp1 (const PLL& p1, const PLL& p2) {
if (p1.x != p2.x) return p1.x < p2.x;
return p1.y > p2.y;
}
bool cmp2 (const PLI& p1, const PLI& p2) {
return p1.x < p2.x;
}
PLL p[MX];
void print (LL x, int y) {
for (int i = 4; i >= 0; i--) {
LL tmp = x >> (8 * i) & 255;
printf ("%lld%c", tmp, i == 0 ? '/' : '.');
}
printf ("%d\n", y);
}
int main() {
//freopen ("in.txt", "r", stdin);
while (~scanf ("%d", &n), n) {
ver.clear();
for (int i = 0, j; i < n; i++) {
scanf ("%s", str);
int len = strlen (str);
for (j = 0; j < len; j++) if (str[j] == '-' || str[j] == '/') break;
LL l, r;
if (str[j] == '-') {
l = code (0, j), r = code (j + 1, len);
} else {
l = r = code (0, j);
int cnt = 0;
for (++j; j < len; j++) cnt = cnt * 10 + str[j] - '0';
for (int i = cnt + 1; i <= 40; i++) r |= (1LL << (40 - i) );
}
p[i] = PLL (l, r);
}
sort (p, p + n, cmp1);
int pre = 0;
for (int i = 1; i < n; i++) {
if (p[i].x - 1 <= p[pre].y) p[pre].y = max (p[pre].y, p[i].y);
else p[++pre] = p[i];
}
for (int i = 0; i <= pre; i++) t.init(), t.insert (p[i].x), t.insert (p[i].y), t.query();
sort (ver.begin(), ver.end(), cmp2);
printf ("%d\n", ver.size() );
for (auto f : ver) print (f.x, f.y);
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#define N 30010
using namespace std;
int d[N],deep[N],n;
vector<int> g[N];
bool v[N];
void dfs(int x,int fa,int dep)
{
d[x]=dep;
deep[x]=0;
v[x]=(x==1);
for(int i=0;i<g[x].size();i++)
{
int y=g[x][i];
if (y==fa) continue;
dfs(y,x,dep+1); v[x]|=v[y];
deep[x]=max(deep[x],deep[y]+1);
}
}
void getsub(int x,int fa,int &sub)
{
int fir=0,sec=0;
for(int i=0;i<g[x].size();i++)
{
int y=g[x][i];
if (y==fa) continue;
if (deep[y]+1>fir)
{
sec=fir,fir=deep[y]+1;
sub=max(sec,sub);
} else
{
sec=max(sec,deep[y]+1);
sub=max(sec,sub);
}
getsub(y,x,sub);
}
}
void solve(int x,int fa,int &k,int c)
{
if (x==1) return;
if (d[1]-d[x]<k) return;
int fir=0,sec=0,nxt,z;
for(int i=0;i<g[x].size();i++)
{
int y=g[x][i];
if (fa==y) continue;
if (v[y]) {nxt=y;continue;}
if (deep[y]+1>fir) sec=fir,fir=deep[y]+1,z=y;
else sec=max(sec,deep[y]+1);
}
int h=d[1]-d[x];
if (fir)
{
int sub=0;
int tmp=max(c,sec);
if (tmp<fir)
{
getsub(z,x,sub);
tmp=max(tmp,sub);
if (tmp) k=max(k,tmp+1);
} else k=max(k,fir+1);
k=min(k,h);
}
solve(nxt,x,k,max(fir,c)+1);
}
int main()
{
while(~scanf("%d",&n))
{
if (n==0) break;
for(int i=0;i<=n;i++) g[i].clear();
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(n,0,0);
int ans=0;
solve(n,0,ans,0);
printf("%d\n",ans);
}
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<map>
#include<queue>
#define N 20010
using namespace std;
struct Suffix_Automation
{
int tot,cur;
struct node{int ch[2],len,fa;} T[N];
void init(){cur=tot=1;memset(T,0,sizeof(T));}
void ins(int x,int id)
{
int p=cur;cur=++tot;T[cur].len=id;
for(;p&&!T[p].ch[x];p=T[p].fa) T[p].ch[x]=cur;
if (!p) {T[cur].fa=1;return;}int q=T[p].ch[x];
if (T[p].len+1==T[q].len){T[cur].fa=q;return;}
int np=++tot; memcpy(T[np].ch,T[q].ch,sizeof(T[q].ch));
T[np].fa=T[q].fa; T[q].fa=T[cur].fa=np; T[np].len=T[p].len+1;
for(;p&&T[p].ch[x]==q;p=T[p].fa) T[p].ch[x]=np;
}
} SAM;
struct node{int pos,t,i;};
char s[N],st[N];
int n,m,len,ans;
void dfs(int x,int pos,int t)
{
if (t>=ans) return;
if (x>=len) return;
int ch=st[x]-'0';
int nxt=SAM.T[pos].ch[ch];
if (nxt==0) ans=min(ans,t);
else dfs(x+1,nxt,t);
nxt=SAM.T[pos].ch[ch^1];
if (nxt==0) ans=min(ans,t+1);
else dfs(x+1,nxt,t+1);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
SAM.init();
if (n+m==0) break;
scanf("%s",s);
for(int i=0;s[i];i++)
SAM.ins(s[i]-'0',i+1);
while(m--)
{
int a,b;
ans=0x3f3f3f3f;
scanf("%d %d",&a,&b);
len=b-a+1; a--; b--;
for(int i=0;i<len;i++)
st[i]=s[a+i];
st[b-a+1]='\0'; dfs(0,1,0);
if (ans==0x3f3f3f3f) puts("Impossible");
else printf("%d\n",ans);
}
}
}
相關推薦
CSU 2034: Column Addition 2035: Cafe Bazaar 2036: Getting Back Home 2037: Mars
#include <cstdio> #include<map> #include<algorithm> #include<cstring> #include<iostream> #define lson l,m,rt<<1 #define
CSU-2034 Column Addition
sin mxd int war each 題解 保留 p2s pbr CSU-2034 Column Addition Description A multi-digit column addition is a formula on adding two integers
Column Addition~DP(腦子抽了,當時沒有想到)
PV ons vts nba can pua 12c pau 圖片 Description A multi-digit column addition is a formula on adding two integers written like this: A mu
UPC5431/acm icpc 2017 Tehran Column Addition
pla mage column IT scanf can 題目 可能 print 題目鏈接:http://exam.upc.edu.cn/problem.php?cid=1326&pid=7 題意:給你一個可能存在錯誤的加法等式,問最少刪除多少列能使等式成立。 eg
【動態規劃】Column Addition @ICPC2017Tehran/upcexam5434
時間限制: 1 Sec 記憶體限制: 128 MB 題目描述 A multi-digit column addition is a formula on adding two integers written like this:
CSU 2037 Mars(字尾自動機+DFS)
Description A new form of life is recently discovered on Mars. Every alien has a DNA, that is a string with an alphabet of only two, rather than four, l
解決mysql報錯:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'
_for tran contains column schema mysql eat table express mysql執行報錯: - Expression #1 of ORDER BY clause is not in GROUP BY clause and cont
CSU - 1556 Jerry's trouble(高速冪取模)
click ostream algo printf 高速 ron main 取模 bit 【題目鏈接】:click here 【題目大意】:計算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 &
(樹形dp+LCA倍增法)CSU 1915 - John and his farm
const http 題解 def iostream clu algo farm john 題意: 有一個棵樹,現在讓你找兩個點連接起來,這樣必然成為一個環,現在要求這些環長度的期望,也就是平均值。 分析: 第一次做LCA題,做多校的時候,瞎幾把找了模板敲,敲了個八九
csu-acm 1503: 點到圓弧的距離
tdi per 註釋 -s 最短距離 txt href amp code 1503: 點到圓弧的距離 分析: 先判斷點和圓心的連線是否在圓弧範圍內,如果在,最短距離即到圓心的距離減去半徑的絕對值;反之,為到端點的最短距離。 具體看註釋 #include <
[HDU]2036:改革春風吹滿地
ext math cto return courier int 數據 改革春風吹滿地 ber Problem Description “ 改革春風吹滿地,不會AC沒關系;實在不行回老家,還有一畝三分地。謝謝!(樂隊奏樂)”話說部分學生心態極好,每天就知道遊戲,這次考試如此簡
(線段樹區間賦值)CSU 1942 - Sort String
== print 字母 can right += 小寫 csu 一模一樣 題意: 一個串(串中只有26個小寫字母),選一個區間進行排序,進行100000次,輸出最後的串。 分析: 比賽的時候很懵逼,感覺這題跟之前的額大崩龍有點像,但是沒多想,也怪自己太菜了。
[LeetCode] Range Addition II 範圍相加之二
ted won not example res date imu pla amp Given an m * n matrix M initialized with all 0‘s and several update operations. Operations a
easyUI的column的field的顏色屬性
width wid idt dex red nbsp val field title {field:‘hasPrintStr‘,title:‘狀態‘,width:10,halign:‘center‘,align:‘right‘,styler: function(value
Excel Sheet Column Number
ble for script con ber leetcode column ole excel https://leetcode.com/problems/excel-sheet-column-number/#/description 另一個atoi /**
導入轉儲文件的時候:Error Code: 1406. Data too long for column - MySQL
some sql_mod ict and query long switch oba -m MySQL will truncate any insert value that exceeds the specified column width. to make this
POJ 2248 Addition Chains dfs(水)
true const bool chains addition blog 條件 題意 sin 題意:給出n 構成出滿足下列條件 長度最小的數列a[0]=1,a[m]=n, a[0]<a[1]<..<a[m]每個下標k都存在(i,j<k) 滿足:a[k
解決CSS3多列樣式column-width布局時內容被截斷、錯亂
hive css3多列 none 技術分享 技術 ive cor title log 一、問題 使用CSS3的column-width: auto | <length>屬性時,出現排列的內容被截斷,出現錯位的現象。 二、原因 需要為圖片容器設置高度,不
LeetCode 598. Range Addition II (區域範圍內加法)
res val 標簽 pub 所有 ive time mat nat Given an m * n matrix M initialized with all 0‘s and several update operations. Operations are repre
Mysql 插入時間時報錯Incorrect datetime value: '' for column 'createtime'
不能 mysql 可能 amp 數據庫 eol 版本更新 都是 pan 在網上找了很多方法總結如下: 1.MySQL驅動版本的問題。這種一般是在mYSQL版本更新了之後才會報錯。解決方法在jdbc裏添加“&useOldAliasMetadataBehavior=tr