Powered By HC TECH : AutoACer Engine

This commit is contained in:
Kirigaya Kazuto 2016-08-13 23:51:27 +08:00 committed by GitHub
parent e254c2bb97
commit 378c82e513
89 changed files with 5110 additions and 0 deletions

51
HDOJ/1300_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;
#define MAXN 1100
typedef struct kind
{
int num,price;
}Kind;
Kind kind[MAXN];
int sum[MAXN];
int ans[MAXN];
int main()
{
int t;
int i,j;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sum[0]=0;
memset(ans,0,sizeof(ans));
for(i=1;i<=n;i++)
{
scanf("%d%d",&kind[i].num,&kind[i].price);
sum[i]=sum[i-1]+kind[i].num;
ans[i]=(sum[i]+10)*kind[i].price;
}
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
ans[i]=min(ans[i],(sum[i]-sum[j]+10)*kind[i].price+ans[j]);
printf("%d\n",ans[n]);
}
return 0;
}

63
HDOJ/1301_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <string.h>
#define inf 0x6fffff
int dis[105];
int map[105][105];
int vis[105];
int sum,n;
void prim()
{
int i,j,k,min;
for(i=1;i<=n;i++)
dis[i]=map[1][i];
vis[1]=1;
for(i=1;i<=n;i++)
{
min=inf;
for(j=1;j<=n;j++)
{
if(vis[j]==0 &&dis[j]<min)
{
min=dis[j];
k=j;
}
}
if(min==inf)
break;
vis[k]=1;
sum+=min;
for(j=1;j<=n;j++)
{
if(vis[j]==0 &&dis[j]>map[k][j])
dis[j]=map[k][j];
}
}
}
int main()
{
int i,j,a,l;
char b[22],b1[22];
while(scanf("%d",&n)!=EOF&&n!=0)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=inf;
for(i=1;i<=n-1;i++)
{
scanf("%s",b);
scanf("%d",&a);
for(j=1;j<=a;j++)
{
scanf("%s",b1);
scanf("%d",&l);
map[b[0]-'A'+1][b1[0]-'A'+1]=map[b1[0]-'A'+1][b[0]-'A'+1]=l;
}
getchar();
}
memset(vis,0,sizeof(vis));
sum=0;
prim();
printf("%d\n",sum);
}
return 0;
}

28
HDOJ/1302_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<iostream>
using namespace std;
int main()
{
int h,u,d,f;
while(cin>>h>>u>>d>>f&&h!=0)
{
int i=1;
double sum=0;
while(i)
{
sum+=u-u*(i-1)*double(f)/100;
if(sum>h)
{
cout<<"success on day "<<i<<endl;
break;
}
sum-=d;
if(sum<0)
{
cout<<"failure on day "<<i<<endl;
break;
}
i++;
}
}
return 0;
}

24
HDOJ/1303_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int temp;
while(cin>>temp&&temp!=-1)
{
vector<int> v;
v.push_back(temp);
while(cin>>temp&&temp!=0)
v.push_back(temp);
int sum=0;
for(int i=0;i!=v.size();++i)
{
auto b=find(v.cbegin(),v.cend(),2*v[i]);
if(b!=v.cend())
sum++;
}
cout<<sum<<endl;
}
return 0;
}

32
HDOJ/1304_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name1,name0;
int n,i,submit,time;
int solve1,solve0=0,penalty1,penalty0=0;
cin>>n;
while(n--)
{
cin>>name1;
solve1=penalty1=0;
for(i=0;i<4;i++)
{
cin>>submit>>time;
if(time)
{
solve1++;
penalty1=penalty1+time+(submit-1)*20;
}
}
if(solve1>solve0||(solve1==solve0&&penalty1<penalty0))
{
name0=name1;
solve0=solve1;
penalty0=penalty1;
}
}
cout<<name0<<" "<<solve0<<" "<<penalty0<<endl;
return 0;
}

77
HDOJ/1305_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct Trie
{
int v;
Trie* next[2];
};
Trie* root;
void creatTrie(char str[])
{
Trie* p=root,*q;
int i;
for(i=0;i<strlen(str);i++)
{
int id=str[i]-'0';
if(p->next[id]==NULL)
{
q=(Trie*)malloc(sizeof(Trie));
q->next[0]=q->next[1]=NULL;
q->v=1;
p->next[id]=q;
}
p=p->next[id];
}
p->v=-1;
}
int findTrie(char str[])
{
Trie* p=root;
for(int i=0;i<strlen(str);i++)
{
int id=str[i]-'0';
if(i<strlen(str)-1)
{
if(p->next[id]->v==-1)
{
return 1;
}
}
p=p->next[id];
}
return 0;
}
int main()
{
char s[10][20];
int c=1;
while(scanf("%s",s[0])!=EOF)
{
root=(Trie*)malloc(sizeof(Trie));
root->next[0]=NULL;
root->next[1]=NULL;
root->v=1;
creatTrie(s[0]);
int k=1;
while(scanf("%s",s[k])!=EOF&&s[k][0]!='9')
{
creatTrie(s[k++]);
}
int i;
for( i=0;i<k;i++)
{
int flag=findTrie(s[i]);
if(flag)
{
printf("Set %d is not immediately decodable\n",c++);
break;
}
}
if(i>=k)
{
printf("Set %d is immediately decodable\n",c++);
}
}
return 0;
}

57
HDOJ/1306_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
string s1,s2,t1,t2,x1,x2;
int i,j,k,n;
while(cin>>x1&&x1!="-1")
{
cin>>x2;
if(x1.length()>x2.length())
{
s1=x2;
s2=x1;
}
else
{
s1=x1;
s2=x2;
}
int sum=0;
for(i=0;i<s1.length();i++)
for(k=0;k<s2.length();k++)
for(j=1;j<=s1.length()-i&&j<=s2.length()-k;j++)
{
int total=0;
t1=s1.substr(i,j);
t2=s2.substr(k,j);
for(n=0;n<j;n++)
if(t1[n]==t2[n])
total++;
if(total>sum)
sum=total;
}
int temp=x1.length()+x2.length();
int t=sum*2;
if(sum==0)
cout<<"appx("<<x1<<","<<x2<<")"<<" = "<<"0"<<endl;
else if(t==temp)
cout<<"appx("<<x1<<","<<x2<<")"<<" = "<<"1"<<endl;
else
{
for(i=2;i<temp;i++)
if(temp%i==0&&t%i==0)
{
while(temp%i==0&&t%i==0)
{
t/=i;
temp/=i;
}
}
cout<<"appx("<<x1<<","<<x2<<")"<<" = "<<t<<"/"<<temp<<endl;
}
}
return 0;
}

109
HDOJ/1307_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<list>
#include<vector>
using namespace std;
int n;
int from[1000][11];
int to[1000][11];
int l;
int s[11],e[11];
bool vist[100000];
bool ans;
int get(int f[])
{
int sum=0;
for (int i=0;i<n;i++)
sum+=i*11+f[i];
return sum;
}
bool yes(int *a,int *b)
{
for (int i=0;i<n;i++)
{
if (a[i]!=b[i])
{
return false;
}
}
return true;
}
void dfs(int *f)
{
int i,j,k;
if (yes(f,e))
{
ans=true;
return ;
}
for (i=0;i<l;i++)
{
k=get(from[i]);
if (!vist[k]&&yes(from[i],f))
{
vist[k]=true;
dfs(to[i]);
vist[k]=false;
}
k=get(to[i]);
if (!vist[k]&&yes(to[i],f))
{
vist[k]=true;
dfs(from[i]);
vist[k]=false;
}
}
}
void put()
{
int i,j,k;
for (i=0;i<l;i++)
{
for (j=0 ;j<n;j++)
cout<<from[i][j]<<' ';
for (j=0;j<n;j++)
cout<<to[i][j]<<' ';
cout<<endl;
}
}
int main()
{
int i,j,k;
int num=0;
while (cin>>n,n)
{
num++;
l=0;
for (i=0;i<n;i++)
cin>>s[i];
for (i=0;i<n;i++)
cin>>e[i];
while (cin>>k)
{
if (k<0)
break;
from[l][0]=k;
for (i=1;i<n;i++)
cin>>from[l][i];
for (i=0;i<n;i++)
cin>>to[l][i];
l++;
}
memset(vist,false,sizeof(vist));
ans=false;
dfs(s);
if (ans)
printf("Maze #%d can be travelled\n",num);
else
printf("Maze #%d cannot be travelled\n",num);
}
}

101
HDOJ/1308_autoAC.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <stdio.h>
int calculate(int year, int month, int day);
int main()
{
const char *weekdays[] =
{
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
const char *monthname[] =
{
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
int year, month, day, week;
while (scanf("%d%d%d", &month, &day, &year) != EOF)
{
if (year == 0 && month == 0 && day == 0)
break;
week = calculate(year, month, day);
if (week == -1)
printf("%d/%d/%d is an invalid date.\n", month, day, year);
else
printf("%s %d, %d is a %s\n", monthname[month - 1], day, year, weekdays[week]);
}
return 0;
}
int calculate(int year, int month, int day)
{
int count, leap, century;
static const int daycount[] =
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
if (year <= 0 || month > 12 || month < 1 || day < 1 || day > 31)
return -1;
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
return -1;
if (year <= 1752)
{
if (year == 1752 && month == 9 && day > 2 && day < 14)
return -1;
if (year % 4 == 0)
{
leap = 1;
if (month == 2)
{
if (day > 29)
return -1;
}
}
else
{
leap = 0;
if (month == 2)
{
if (day > 28)
return -1;
}
}
count = (year - 1) * 365 + (year - 1) / 4;
count += daycount[month - 1] + day;
if (leap && month > 2)
count += 1;
if (year == 1752)
{
if (month > 9 || (month == 9 && day >= 14))
count -= 11;
}
return ((count + 5) % 7);
}
else
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
leap = 1;
if (month == 2)
{
if (day > 29)
return -1;
}
}
else
{
leap = 0;
if (month == 2)
{
if (day > 28)
return -1;
}
}
century = (year - 1) / 100 - 17;
count = (year - 1) * 365 + (year - 1) / 4;
count += (daycount[month - 1] + day);
count -= (11 + century);
count += (century + 1) / 4;
if (leap && month > 2)
count += 1;
return ((count + 5) % 7);
}
return -1;
}

31
HDOJ/1309_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
double mon,downpay,loan,dep;
double rate[110];
int main(){
// freopen("in","r",stdin);
while(cin >> mon >> downpay>>loan>>dep&&mon>0){
memset(rate,0.0,sizeof rate);
while(dep--){
int s;
double ra;
cin >> s >> ra;
for(int i = s; i <= 100; i++) rate[i] = ra;
}
int ans = 0;
double aver = loan/mon;
double nowloan = loan;
double nowsum = (loan+downpay)*(1-rate[0]);
while(nowsum < nowloan){
ans++;
nowloan -= aver;
nowsum = nowsum*(1-rate[ans]);
}
cout<<ans<<" ";
if(ans != 1) cout<<"months"<<endl;
else cout<<"month"<<endl;
}
return 0;
}

72
HDOJ/1310_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdio>
#include <cstring>
using namespace std;
#define ONLINE_JUDGE
#define MAXN 125
#define INF 0xfffffff
int nn;
int dis[MAXN][MAXN];
string buf[MAXN];
bool visit[5];
map<string, int> tb;
int a[MAXN];
int myabs(int x) {
return x<0 ? -x:x;
}
void init() {
string s = "ABCDE";
nn = 0;
int i, j, k, p;
int sum, tmp;
do {
buf[nn] = s;
tb[s] = nn++;
} while (next_permutation(s.begin(), s.end()));
for (i=0; i<nn; ++i) {
dis[i][i] = 0;
for (j=i+1; j<nn; ++j) {
sum = 0;
memset(visit, false, sizeof(visit));
for (k=0; k<5; ++k) {
for (p=0; p<5; ++p) {
if (buf[j][p] == buf[i][k])
break;
else if (!visit[buf[j][p]-'A'])
++sum;
}
visit[buf[i][k]-'A'] = true;
}
dis[i][j] = dis[j][i] = sum;
}
}
}
int main() {
int n;
int i, j, k, tmp;
int v, mmin, sum;
string s;
ios::sync_with_stdio(false);
init();
while (cin>>n && n) {
for (i=0; i<n; ++i) {
cin >>s;
a[i] = tb[s];
}
mmin = INF;
for (i=0; i<nn; ++i) {
sum = 0;
for (j=0; j<n; ++j)
sum += dis[i][a[j]];
if (sum < mmin) {
v = i;
mmin = sum;
}
}
printf("%s is the median ranking with value %d.\n", buf[v].c_str(), mmin);
}
return 0;
}

60
HDOJ/1311_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
#include <set>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int father[110];
struct data
{
string father, child;
int dis;
int age;
};
int n;
data family[200];
bool sign[200];
void dfs(string str, int year)
{
for(int i=0; i<n; i++)
{
if(family[i].father == str && !sign[i])
{
family[i].age = year - family[i].dis;;
sign[i] = 1;
dfs(family[i].child, family[i].age);
}
}
}
bool cmp(data a, data b)
{
if(a.age != b.age)
return a.age > b.age ? 1:0;
else
return a.child < b.child ? 1:0;
}
int main()
{
int T;
int co = 1;
scanf("%d\n", &T);
while(T--)
{
printf("DATASET %d\n", co++);
scanf("%d", &n);
int flag;
memset(sign, 0, sizeof(sign) );
for(int i=0; i<n; i++)
{
cin >> family[i].father >> family[i].child >> family[i].dis;
}
dfs("Ted", 100);
sort(family, family+n, cmp);
for(int i=0; i<n; i++)
cout << family[i].child << ' ' << family[i].age << endl;
}
return 0;
}

71
HDOJ/1312_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include<stdio.h>
#include<string.h>
char map[100][100];
int sign[100][100];
int n,m;
int ans;
void dfs(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
{
if(x-1>=0&&map[x-1][y]=='.'&&sign[x-1][y]==0)
{
sign[x-1][y]=1;
ans++;
dfs(x-1,y);
}
if(x+1<n&&map[x+1][y]=='.'&&sign[x+1][y]==0)
{
sign[x+1][y]=1;
ans++;
dfs(x+1,y);
}
if(y-1>=0&&map[x][y-1]=='.'&&sign[x][y-1]==0)
{
sign[x][y-1]=1;
ans++;
dfs(x,y-1);
}
if(y+1<m&&map[x][y+1]=='.'&&sign[x][y+1]==0)
{
sign[x][y+1]=1;
ans++;
dfs(x,y+1);
}
}
return;
}
int main()
{
while(scanf("%d %d\n",&m,&n)!=EOF)
{
if(m==0&&n==0)
{
break;
}
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
}
int x,y;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='@')
{
x=i;
y=j;
i+=100000000;
j+=100000000;
}
}
}
memset(sign,0,sizeof(sign));
sign[x][y]=1;
ans=1;
dfs(x,y);
printf("%d\n",ans);
}
return 0;
}

49
HDOJ/1313_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
int main(void)
{
char number[61];
int value[60];
int result[60];
int i,j,k,l;
int flag;
while(gets(number))
{
int n = strlen(number);
for(i = 0; i < n; i++)
value[i] = number[i] - '0';
for(i = 1; i <= n; i++)
{
flag = 0;
int r = 0;
for(j = n - 1; j >= 0; j--)
{
r += value[j] * i;
result[j] = r % 10;
r /= 10;
}
for(j = 0; j < n; j++)
{
if(result[j] == value[0])
{
k = j;
for(l = 0; l < n; l++)
{
if(result[k] != value[l])
break;
k == n - 1 ? k = 0:k++;
}
if(l == n)
{
flag = 1;
break;
}
}
}
if(flag == 0)
break;
}
printf("%s is %scyclic\n",number,flag == 0? "not ":"");
}
return 0;
}

103
HDOJ/1314_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<cstdio>
#include<cmath>
#include<cstring>
int a[40], b[40], ca[40];
void add(int s[], int n) {
int t = 0, i;
s[39] += n;
for (i = 39; i >= 0; --i) {
s[i] += t;
t = s[i] / 10;
s[i] %= 10;
}
}
void mul(int s[], int n) {
int t = 0, i;
for (i = 39; i >= 0; --i) {
s[i] = s[i] * n + t;
t = s[i] / 10;
s[i] %= 10;
}
}
void sub(int s[], int n) {
int t = 0, i;
s[39] -= n;
for (i = 39; i >= 0; --i) {
if (s[i] >= 0) {
break;
} else {
s[i + 1]--;
s[i] += 10;
}
}
}
int div_mod(int s[], int n) {
int t = 0, i;
for (i = 0; i < 40; ++i) {
s[i] += 10 * t;
t = s[i] % n;
s[i] /= n;
}
return t;
}
int is_zero(int s[]) {
int i;
for (i = 0; i < 40; ++i)
if (s[i])
return 0;
return 1;
}
char ss[40];
int main() {
int le, i, ans;
while (scanf("%s", ss) != EOF) {
if (ss[0] == '*')
break;
le = strlen(ss);
memset(a, 0, sizeof (a));
memset(b, 0, sizeof (b));
memset(ca, 0, sizeof (ca));
if (ss[0] <= '9' && ss[0] >= '0') {
for (i = 39; le > 0; --i)
ca[i] = a[i] = ss[--le] - '0';
for (i = 39; i >= 0; --i) {
ans = div_mod(a, 26);
if (!ans) {
b[i] = 26;
sub(a, 1);
} else
b[i] = ans;
if (is_zero(a))
break;
}
for (i = 0; i < 40; ++i)
a[i] = ca[i];
} else if (ss[0] <= 'z' && ss[0] >= 'a') {
for (i = 39; le > 0; --i)
b[i] = ss[--le] - 'a' + 1;
for (i = 0; i < 40; ++i) {
mul(a, 26);
add(a, b[i]);
}
}
for (i = 0; i < 40; ++i)
if (b[i])
break;
ans = i;
for (; i < 40; ++i)
printf("%c", b[i] + 'a' - 1);
ans -= 18;
while (ans--)
printf(" ");
for (i = 0; i < 40; ++i)
if (a[i])
break;
for (; i < 40; ++i) {
printf("%c", a[i] + '0');
if (i < 39 && i % 3 == 0)
printf(",");
}
printf("\n");
}
return 0;
}

40
HDOJ/1315_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
char s[100];
int pd(double p,int i)
{
int j;
double g=0;
if(p<2||p>16) return 0;
if(s[i]=='#') return 0;
for(j=i;s[j]!='\0';j++)
{
if(s[j]=='#') break;
if(p>10&&(s[j]<'0'||(s[j]>'9'&&s[j]<'a')||s[j]>'a'+p-11)) return 0;
if(p<=10&&!(s[j]>='0'&&s[j]<='0'+p-1)) return 0;
if(s[j]>='a'&&s[j]<='f') g=g*p+10+s[j]-'a';
else g=g*p+s[j]-'0';
}
if(s[j]=='#'&&s[j+1]=='\0'||s[j]=='#'&&s[j+1]=='#'&&pd(g,j+2))
return 1;
else return 0;
}
int main()
{
int t,j;
double p;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
p=0;
for(j=0;;j++)
{
if(s[j]>='0'&&s[j]<='9')
p=p*10+s[j]-'0';
else break;
}
if(s[j]=='#'&&pd(p,j+1)||p==0&&s[j]=='\0') printf("yes\n");
else printf("no\n");
}
return 0;
}

83
HDOJ/1316_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define NUM 500
#define MAX_LEN 110
class BigInteger{
int d[MAX_LEN];
int len;
public:
BigInteger(){
memset( d, 0, sizeof(d));
len = 0;
}
void set(char *str){
len = strlen(str);
for(int i = 0, j = len - 1; i < len; i++, j-- ){
d[i] = str[j] - '0';
}
}
BigInteger operator+(const BigInteger &other)const{
BigInteger ret;
ret.len = max( len, other.len );
for(int i = 0; i < ret.len; i++ ){
ret.d[i] = d[i] + other.d[i];
}
for(int i = 0; i < ret.len; i++ ){
if(ret.d[i] > 9){
ret.d[i] -= 10;
ret.d[i + 1]++;
}
}
if(ret.d[ret.len] > 0)
ret.len ++;
return ret;
}
bool operator<=(const BigInteger &other)const{
if(len < other.len)
return true;
if(len == other.len){
int i = len - 1;
while(i >= 0 && d[i] == other.d[i])
i--;
if(i == -1 || d[i] <= other.d[i])
return true;
}
return false;
}
};
BigInteger fibo[NUM], a, b;
char stra[MAX_LEN], strb[MAX_LEN];
void init(){
fibo[0].set("1"); fibo[1].set("2");
for(int i = 2; i < NUM; i++ )
fibo[i] = fibo[i - 1] + fibo[i - 2];
}
bool input(){
scanf("%s%s", stra, strb );
if(stra[0] == '0' && strb[0] == '0')
return false;
a.set(stra); b.set(strb);
return true;
}
void output(){
int l, r;
for( l = 0; l < NUM; l++ ){
if(a <= fibo[l])
break;
}
for( r = NUM - 1; r > 0; r-- ){
if(fibo[r] <= b)
break;
}
printf("%d\n", r - l + 1 );
}
int main(){
init();
while(input()){
output();
}
return 0;
}

99
HDOJ/1317_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn=102;
struct node
{
int to;
int next;
};
node edge[maxn*maxn];
node edge1[maxn*maxn];
int head[maxn];
int head1[maxn];
bool ac[maxn];
int isin[maxn];
int point[maxn];
int total[maxn];
int stack[maxn+10];
void dfs(int x)
{
isin[x]=1;
ac[x]=true;
for(int k=head1[x];k!=-1;k=edge1[k].next)
{
if(!isin[edge1[k].to])
dfs(edge1[k].to);
}
}
int main()
{
int n,i,j,k,t,x,c,ptr,top;
while(scanf("%d",&n)==1)
{
if(n==-1) break;
if(n==1)
{
printf("winnable\n");
continue;
}
for(i=1;i<=n;i++)
{
head[i]=-1;
head1[i]=-1;
total[i]=-1;
isin[i]=0;
ac[i]=false;
}
c=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&point[i],&t);
for(j=1;j<=t;j++)
{
scanf("%d",&x);
edge[c].to=x;
edge[c].next=head[i];
head[i]=c;
edge1[c].to=i;
edge1[c].next=head1[x];
head1[x]=c++;
}
}
dfs(n);
if(!ac[1]) printf("hopeless\n");
else
{
for(i=1;i<=n;i++) isin[i]=0;
bool ju=false;
total[1]=100;
ptr=0;
stack[++ptr]=1;
while(ptr&&!ju)
{
top=stack[ptr--];
isin[top]=2;
for(k=head[top];k!=-1&&!ju;k=edge[k].next)
{
if(ac[edge[k].to])
{
int temp=total[top]+point[edge[k].to];
if(temp>0&&temp>total[edge[k].to])
{
total[edge[k].to]=temp;
if(edge[k].to==n||isin[edge[k].to]==2) ju=true;
else if(!isin[edge[k].to])
{
stack[++ptr]=edge[k].to;
isin[edge[k].to]=1;
}
}
}
}
}
if(ju) printf("winnable\n");
else printf("hopeless\n");
}
}
return 0;
}

13
HDOJ/1318_autoAC.cpp Normal file
View File

@ -0,0 +1,13 @@
Character Reverse Character Reverse Character Reverse
A A M M Y Y
B N Z 5
C O O 1 1
D P 2 S
E 3 Q 3 E
F R 4
G S 2 5 Z
H H T T 6
I I U U 7
J L V V 8 8
K W W 9
L J X X

31
HDOJ/1319_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int p[1001],vis[1001],k=0;
memset(vis,0,sizeof(vis));
memset(p,0,sizeof(p));
for(int i=2; i*i<=1000; i++)
for(int j=i*i; j<=1000; j+=i)
vis[j]=1;
for(int i=1; i<=1000; i++)
if(!vis[i])
p[k++]=i;
int n,m;
while(cin>>n>>m)
{
int sum=0;
for(int i=1; i<=n; i++)
if(!vis[i])
sum++;
int c=(sum>>1)-m+(sum&1);
c=(c<0)?0:c;
printf("%d %d:", n, m);
for(int i=c; i<c+2*m-(sum&1)&&i<sum; i++)
printf(" %d",p[i]);
printf("\n\n");
}
return 0;
}

52
HDOJ/1320_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<iostream>
#include<string>
using namespace std;
int a[100],b[100];
int main()
{
int N,i,j;
while(cin>>N&&N)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
char c;
cin>>c;
if(c=='P')
{
for(i=1;i<=N;i++)
cin>>b[i];
for(i=1;i<=N;i++)
{
for(j=1;j<i;j++)
if(b[j]>b[i])
a[b[i]]++;
}
for(i=1;i<N;i++)
cout<<a[i]<<" ";
cout<<a[N]<<endl;
}
if(c=='I')
{
for(i=1;i<=N;i++)
cin>>a[i];
for(i=1;i<=N;i++)
{
j=1;
int k=1;
while(1)
{
if(b[k]==0&&j>a[i])
break;
if(b[k]==0)
j++;
k++;
}
b[k]=i;
}
for(i=1;i<N;i++)
cout<<b[i]<<" ";
cout<<b[N]<<endl;
}
}
return 0;
}

23
HDOJ/1321_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
char a[100];
int i=0;
while(1)
{
a[i]=getchar();
if(a[i]=='\n')
break;
i++;
}
i--;
for(;i>=0;i--)
putchar(a[i]);
printf("\n");
}
}

18
HDOJ/1322_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
double in;
int out,count = 1;
while(scanf("%lf",&in)!=EOF,in!=0)
{
in *= 0.75;
in /= (1.86*1000000);
out = (int)in;
if(in - out != 0)
{
out++;
}
printf("File #%d\nJohn needs %d floppies.\n\n",count++,out);
}
return 0;
}

36
HDOJ/1323_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<cstdio>
using namespace std;
int t[103];
int judge(int n)
{
int sum=0;
for(int i=1;i<n;++i)
if(n%i==0)
sum+=i;
if(sum==n)
return 0;
if(sum<n)
return -1;
if(sum>n)
return 1;
}
int main()
{
int i,num=0;
while(cin>>t[++num]&&t[num]);
--num;
cout<<"PERFECTION OUTPUT"<<endl;
for(i=1;i<=num;++i)
{
printf("%5d ",t[i]);
switch(judge(t[i]))
{
case 0:cout<<"PERFECT"<<endl;break;
case -1:cout<<"DEFICIENT"<<endl;break;
case 1:cout<<"ABUNDANT"<<endl;break;
}
}
cout<<"END OF OUTPUT"<<endl;
return 0;
}

35
HDOJ/1324_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<iostream>
using namespace std;
int record[100000];
int main()
{
int z,i,m,l,j;
int key=1;
while(cin>>z>>i>>m>>l)
{
if(z==0&&i==0&&m==0&&l==0)
break;
int k=1;
record[0]=l;
int flag=0;
while(1)
{
l=(z*l+i)%m;
for( j=0;j<k;j++)
{
if(record[j]==l)
{
flag=1;
break;
}
}
record[k++]=l;
if(flag)
break;
}
cout<<"Case "<<key<<':'<<' ';
cout<<k-j-1<<endl;
key++;
}
return 0;
}

61
HDOJ/1325_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#include <cstring>
using namespace std;
int father[100010];
int findfather(int x)
{
while (father[x]!=x)
x=father[x];
return x;
}
bool is_father(int m,int n)
{
while (father[m]!=n)
{
if (father[m]==m) return true;
m=father[m];
}
return false;
}
int main()
{
bool e,flag;
int times=0;
int maxx,root,m,n,t;
while (cin >> m >> n && (m>=0 && n>=0))
{
memset(father,0,sizeof(father));
e=false;flag=true;
root=-1;
maxx=0;
while ((m+n)!=0)
{
if (e) cin >> m >> n;
if (m+n==0) break;
if (maxx<m) maxx=m;
if (maxx<n) maxx=n;
e=true;
if (father[m]==0) father[m]=m;
if (father[n]==0) father[n]=n;
if (father[n]!=n) flag=false;
if (is_father(m,n)) father[n]=m;
else flag=false;
}
times++;
cout << "Case " << times << " is ";
if (flag==false) {cout << "not a tree." << endl;continue;}
for (int i=1;i<=maxx;i++)
{
if (father[i]==0) continue;
t=findfather(i);
if (t!=root)
{
if (root==-1) root=t;
else {flag=false;break;}
}
}
if (flag==false) {cout << "not a tree." << endl;continue;}
cout << "a tree." <<endl;
}
return 0;
}

24
HDOJ/1326_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main()
{
int n,t=0,i,sum,a[100];
int flag;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
t++;
for(i=0,sum=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
sum=sum/n;
for(i=0,flag=0;i<n;i++)
{
if(a[i]>sum)
flag=flag+a[i]-sum;
}
printf("Set #%d\nThe minimum number of moves is %d.\n\n",t,flag);
}
return 0;
}

41
HDOJ/1327_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
using namespace std;
bool al[26]={false};
int main()
{
int n;
int T=1;
while(cin>>n&&n!=0)
{
memset(al,0,sizeof(al));
al[0]=1;
char c1,c2,c3;
while(n--)
{
cin>>c1>>c2>>c3;
if(al[c3-'a']==0)
al[c1-'a']=0;
else
{
al[c1-'a']=1;
}
}
int ok=0;
cout<<"Program #"<<T<<endl;
for(int i=0;i<26;i++)
{
if(al[i])
{
ok=1;
cout<<(char)(i+'a')<<" ";
}
}
if(ok==1)
cout<<endl;
if(ok==0)
cout<<"none"<<endl;
cout<<endl;
T++;
}
return 0;
}

23
HDOJ/1328_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <string.h>
int main()
{
int n,m,i,p,t;
char a[1000];
scanf("%d",&n);
getchar();
for(i=1;i<=n;i++)
{
gets(a);
p=strlen(a);
printf("String #%d\n",i);
for(t=0;t<p;t++)
{
if(a[t]!='Z')
printf("%c",a[t]+1);
else
printf("A");
}
printf("\n\n");
}
}

46
HDOJ/1329_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#define MAXN 50
int main(int ac, char *av[])
{
vector < vector < int > > number;
vector < int > result;
int pegs = 1;
number.resize(MAXN + 2);
for (int i = 1; i <= MAXN; i++)
number[i].push_back(0);
for (int current = 1; pegs <= MAXN;)
{
bool successed = false;
for (int c = 1; c <= pegs; c++)
{
int top = current + number[c][number[c].size() - 1];
int value = sqrt(top);
if (value * value == top)
{
number[c].push_back(current);
successed = true;
break;
}
}
if (successed == false)
{
result.push_back(current - 1);
pegs++;
number[pegs].push_back(current);
current++;
}
else
current++;
}
int cases, n;
cin >> cases;
while (cases--)
{
cin >> n;
cout << result[n - 1] << endl;
}
return 0;
}

24
HDOJ/1330_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int n,i;
double sum;
int f=1;
while(scanf("%d",&n)!=EOF)
{
if(f)
{
printf("# Cards Overhang\n");f=0;
}
sum=0;
for(i=1;i<=n;i++)
{
sum=sum+1.0/(2*i);
}
printf("%5d %.3lf\n",n,sum);
}
return 0;
}

37
HDOJ/1331_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<iostream>
using namespace std;
int x[21][21][21]={0};
int w(int a,int b,int c)
{
if(a<=0||b<=0||c<=0)
return 1;
if(x[a][b][c]!=0)
return x[a][b][c];
if(a<b&&b<c)
{
x[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
return x[a][b][c];
}
else
{
x[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
return x[a][b][c];
}
}
int main()
{
int a,b,c;
while(cin>>a>>b>>c&&!(a==-1&&b==-1&&c==-1))
{
if(a<=0||b<=0||c<=0)
{
printf("w(%d, %d, %d) = %d\n",a,b,c,1);
continue;
}
if(a>20||b>20||c>20)
printf("w(%d, %d, %d) = %d\n",a,b,c,w(20,20,20));
else
printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
}
return 0;
}

127
HDOJ/1332_autoAC.cpp Normal file
View File

@ -0,0 +1,127 @@
#include<stdio.h>
int col,s;
void out(int step,int shu)
{
int i,j;
if(step==1)
{
if(shu==1||shu==4)
{
for(i=1;i<=col;i++)
printf(" ");
}
else
{
printf(" ");
for(i=1;i<=col-2;i++)
printf("-");
printf(" ");
}
}
else
if(step==2*s+3)
{
if(shu==1||shu==4||shu==7)
{
for(i=1;i<=col;i++)
printf(" ");
}
else
{
printf(" ");
for(i=1;i<=col-2;i++)
printf("-");
printf(" ");
}
}
else
if(step==(2*s+4)/2)
{
if(shu==1||shu==7||shu==0)
for(i=1;i<=col;i++)
printf(" ");
else
{
printf(" ");
for(i=1;i<=col-2;i++)
printf("-");
printf(" ");
}
}
else
if(step<(2*s+4)/2)
{
if(shu==1||shu==2||shu==7||shu==3)
{
for(i=1;i<col;i++)
printf(" ");
printf("|");
}
else
if(shu==5||shu==6)
{
printf("|");
for(i=1;i<col;i++)
printf(" ");
}
else
{
printf("|");
for(i=1;i<col-1;i++)
printf(" ");
printf("|");
}
}
else
{
if(shu==1||shu==3||shu==4||shu==5||shu==7||shu==9)
{
for(i=1;i<col;i++)
printf(" ");
printf("|");
}
else
if(shu==2)
{
printf("|");
for(i=1;i<col;i++)
printf(" ");
}
else
{
printf("|");
for(i=1;i<col-1;i++)
{
printf(" ");
}
printf("|");
}
}
}
int main()
{
int n;
int t[20];
while(scanf("%d%d",&s,&n)==2&&s&&n)
{
int i,j,k,x=0;
while(n>0)
{
t[x++]=n%10;
n/=10;
}
col=2+s;
for(i=1;i<=2*s+3;i++)
{
for(j=x-1;j>=0;j--)
{
out(i,t[j]);
if(j>0)
printf(" ");
}
printf("\n");
}
printf("\n");
}
return 0;
}

65
HDOJ/1333_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int n;
bool isprime[10100];
vector<int> prime,a;
int sum(int n)
{
int ans=0;
while(n)
{
ans+=n%10;
n/=10;
}
return ans;
}
void init()
{
memset(isprime,0,sizeof(isprime));
isprime[0]=isprime[1]=true;
for(int i=2;i<=10010;i++)
{
if(isprime[i]==false)
{
prime.push_back(i);
a.push_back(sum(i));
for(int j=i+i;j<=10010;j+=i)
isprime[j]=true;
}
}
}
bool issame(int n)
{
int ans1=sum(n),ans2=0;
if(n<=10010&&isprime[n]==false)
return false;
for(int i=0;i<prime.size()&&prime[i]*prime[i]<=n;i++)
{
while(n%prime[i]==0)
{
ans2+=a[i];n/=prime[i];
}
if(n==1)
break;
}
if(ans2==0) return false;
if(n!=1)
ans2+=sum(n);
if(ans1==ans2)
return true;
return false;
}
int main()
{
init();
while(scanf("%d",&n),n!=0)
{
while(!issame(n+1))
n++;
printf("%d\n",n+1);
}
return 0;
}

21
HDOJ/1334_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
for(int a=3;a!=201;++a)
{
for(int i=2;i<=200;++i)
{
for(int j=i;j<=200;++j)
{
for(int k=j;k<=200;++k)
{
if(i*i*i+j*j*j+k*k*k==a*a*a)
cout<<"Cube = "<<a<<", Triple = ("<<i<<","<<j<<","<<k<<")\n";
}
}
}
}
return 0;
}

58
HDOJ/1335_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
int powe(int,int);
int main()
{
string s;
short b,e;
while(cin>>s>>b>>e)
{
vector<char> v;
string str="";
int sum=0;
for(int i=s.size()-1,c=0;i>=0;--i,++c)
{
if(s[i]>='0'&&s[i]<='9')
sum+=(s[i]-48)*powe(b,c);
if(s[i]>='A'&&s[i]<='F')
sum+=(s[i]-55)*powe(b,c);
}
int r;
while(sum!=0)
{
r=sum%e;
if(r>=0&&r<=9)
v.push_back(r+48);
else if(r==10)
v.push_back('A');
else if(r==11)
v.push_back('B');
else if(r==12)
v.push_back('C');
else if(r==13)
v.push_back('D');
else if(r==14)
v.push_back('E');
else
v.push_back('F');
sum/=e;
}
for(int i=v.size()-1;i>=0;--i)
str+=v[i];
if(str.size()>7)
cout<<setw(7)<<right<<"ERROR"<<endl;
else
cout<<setw(7)<<right<<str<<endl;
}
return 0;
}
int powe(int m,int n)
{
int sum=1;
while(n--)
sum*=m;
return sum;
}

60
HDOJ/1336_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
map<string , int> table;
int main()
{
int cnt = 1;
for(char i = 'a'; i <= 'z'; ++i)
{
string tmp;
tmp += i;
table[tmp] = cnt ++;
}
for(char i = 'a'; i <= 'z'; ++i)
for(char j = i + 1; j <= 'z'; ++j)
{
string tmp;
tmp += i;tmp += j;
table[tmp] = cnt ++;
}
for(char i = 'a'; i <= 'z'; ++i)
for(char j = i + 1; j <= 'z'; ++j)
for(char k = j + 1; k <= 'z'; ++k)
{
string tmp;
tmp += i;tmp += j;tmp += k;
table[tmp] = cnt ++;
}
for(char i = 'a'; i <= 'z'; ++i)
for(char j = i + 1; j <= 'z'; ++j)
for(char k = j + 1; k <= 'z'; ++k)
for(char l = k + 1; l <= 'z'; ++l)
{
string tmp;
tmp += i;tmp += j;tmp += k;tmp += l;
table[tmp] = cnt ++;
}
for(char i = 'a'; i <= 'z'; ++i)
for(char j = i + 1; j <= 'z'; ++j)
for(char k = j + 1; k <= 'z'; ++k)
for(char l = k + 1; l <= 'z'; ++l)
for(char m = l + 1; m <= 'z'; ++m)
{
string tmp;
tmp += i;tmp += j;tmp += k;tmp += l;tmp += m;
table[tmp] = cnt ++;
}
string word;
while(cin >> word)
{
map<string , int>::iterator it = table.find(word);
if(it == table.end())
{cout << 0 << endl;}
else
{cout << it->second << endl;}
}
return 0;
}

33
HDOJ/1337_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int n,a[20000],i,j,k,m,t,f;
while(scanf("%d",&n)!=EOF&&n)
{
while(n--)
{ t=0;
memset(a,0,sizeof(a));
scanf("%d",&m);
f=2;
while(f<=m)
{
for(i=f;i<=m;i+=f)
{
if(a[i]==1)
a[i]=0;
else
a[i]=1;
}
f++;
}
for(i=1;i<=m;i++)
{ if(a[i]==0)
t++;
}
printf("%d\n",t);
}
}
return 0;
}

31
HDOJ/1338_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<iostream>
#include<algorithm>
using namespace std;
int cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,i,j,k;
int a[60];
i=0;
while(cin>>n>>k&&(n||k))
{
i++;
for(j=0;j<k;j++)
cin>>a[j];
sort(a,a+k,cmp);
int max=n*k;
int count=0;
for(j=0;j<k;j++)
if(a[j]==max)
{
count++;
max--;
}
else max-=2;
cout<<"Case "<<i<<':'<<' '<<count<<endl;
}
return 0;
}

19
HDOJ/1339_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t,n,q;
cin>>t;
while(t--)
{
cin>>n;
for(int p=0;p<=log(n)+1;p++)
{
q=1<<p;
if(n%q==0&&((n/q)&1))
cout<<n/q<<" "<<p<<endl;
}
}
return 0;
}

64
HDOJ/1341_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <iostream>
#include <stdio.h>
using namespace std;
unsigned char ins[32], accu, pc;
char str[10];
unsigned char getValue(char *ins)
{
unsigned char res(0);
for(int i(0); i<8; ++i) res = res * 2 + ins[i] - '0';
return res;
}
int main()
{
bool halt;
int type, value;
while(scanf("%s", str) != EOF)
{
ins[0] = getValue(str);
for(int i(1); i<32; ++i) scanf("%s", str), ins[i] = getValue(str);
halt = false;
pc = accu = 0;
while(!halt)
{
type = ins[pc] >> 5;
value = ins[pc] % 32;
switch(type)
{
case 0:
ins[value] = accu;
pc = (pc + 1) % 32;
break;
case 1:
accu = ins[value];
pc = (pc + 1) % 32;
break;
case 2:
if(!accu) pc = value;
else pc = (pc + 1) % 32;
break;
case 3:
pc = (pc + 1) % 32;
break;
case 4:
--accu;
pc = (pc + 1) % 32;
break;
case 5:
++accu;
pc = (pc + 1) % 32;
break;
case 6:
pc = ins[pc] % 32;
break;
case 7:
halt = true;
break;
}
}
for(int i(7); i>=0; --i)
printf("%d", (accu >> i) & 1);
printf("\n");
}
return 0;
}

39
HDOJ/1342_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[20];
int main()
{
int k,i,j,l,m,n,q,t=0;
while (scanf("%d",&k)!=EOF&&k)
{
if (t!=0)
printf ("\n");
t++;
for (i=0;i<k;i++)
{
scanf("%d",&a[i]);
}
sort (a,a+k);
for (i=0;i<k-5&&a[i+1];i++)
{
for (j=i+1;j<k-4&&a[j+1];j++)
{
for (l=j+1;l<k-3&&a[l+1];l++)
{
for (m=l+1;m<k-2&&a[m+1];m++)
{
for (n=m+1;n<k-1&&a[n+1];n++)
{
for (q=n+1;q<k;q++)
{
printf ("%d %d %d %d %d %d\n",a[i],a[j],a[l],a[m],a[n],a[q]);
}
}
}
}
}
}
}
return 0;
}

59
HDOJ/1343_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 2005
int fn, gn, hn, en;
int f[maxn], g[maxn], h[maxn], e[maxn];
void input()
{
scanf("%d", &fn);
for (int i = fn -1; i >=0; i--)
scanf("%d", &f[i]);
scanf("%d", &gn);
for (int i = gn -1; i >=0; i--)
scanf("%d", &g[i]);
scanf("%d", &hn);
for (int i = hn -1; i >=0; i--)
scanf("%d", &h[i]);
}
void mul()
{
memset(e, 0, sizeof(e));
for (int i =0; i < fn; i++)
for (int j =0; j < gn; j++)
e[i + j] ^= f[i] * g[j];
en = fn + gn;
while (en >=0&& e[en] ==0)
en--;
en++;
}
void mo()
{
while (en >= hn)
{
int d = en - hn;
for (int i = hn -1; i >=0; i--)
e[i + d] ^= h[i];
while (en >=0&& e[en] ==0)
en--;
en++;
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
input();
mul();
mo();
printf("%d", en);
for (int i = en -1; i >=0; i--)
printf(" %d", e[i]);
putchar('\n');
}
return 0;
}

74
HDOJ/1344_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 8;
struct Traffic
{
double pos;
int g, y, r;
};
Traffic traffic[N];
int n;
bool input();
void solve();
int main()
{
int t = 1;
while (input()) {
printf("Case %d:", t++);
solve();
}
return 0;
}
bool input()
{
scanf("%d", &n);
if (n < 0) return false;
for (int i = 0; i < n; i++) {
scanf("%lf%d%d%d", &traffic[i].pos, &traffic[i].g, &traffic[i].y, &traffic[i].r);
}
return true;
}
void solve()
{
vector<int> ans;
for (int i = 30; i <= 60; i++) {
bool ok = true;
for (int j = 0; j < n; j++) {
int t = (int)(traffic[j].pos * 3600 / i);
if (t % (traffic[j].g + traffic[j].y + traffic[j].r) >= traffic[j].g + traffic[j].y) {
ok = false;
break;
}
}
if (ok) ans.push_back(i);
}
if (ans.empty()) {
printf(" No acceptable speeds.\n");
} else {
bool first = true;
for (size_ i = 0; i < ans.size(); i++) {
size_ j = i;
while (j + 1 < ans.size() && ans[j + 1] - ans[j] == 1) {
j++;
}
if (first) {
first = false;
if (j - i >= 1) {
printf(" %d-%d", ans[i], ans[j]);
} else {
printf(" %d", ans[i]);
}
} else {
if (j - i >= 1) {
printf(", %d-%d", ans[i], ans[j]);
} else {
printf(", %d", ans[i]);
}
}
i = j;
}
printf("\n");
}
}

107
HDOJ/1345_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
double r[4];
int is_integer(double t)
{
int i=t+0.0000005;
if(fabs(t-i)<0.000001)
return i;
return 0;
}
int try_height(int i,int sw)
{
double dh=sw/r[i];
int h=dh+.0000005;
if(fabs(h-dh)<0.000001)
return h;
return -1;
}
int try_weight(int i,int sh)
{
double dw=sh*r[i];
int w=dw+.0000005;
if(fabs(w-dw)<0.000001)
return w;
return -1;
}
bool fill(int sw, int sh)
{
int w,h,tw,th,j,k,a[4]={0,1,2,3};
for(j=0;j<24;j++)
{
tw=sw,th=sh;
for(k=0;k<4&&th&&tw;k++)
{
h=try_height(a[k],tw);
if(h!=-1&&h<=th)
th-=h;
else
{
w=try_weight(a[k],th);
if(w==-1||w>tw)
break;
tw-=w;
}
}
if (k==4&&(!tw||!th))
return true;
next_permutation(a,a+4);
}
return false;
}
int fit(int w ,int i,int j)
{
double h=w/(r[i]+r[j]);
int ret=is_integer(h);
if(is_integer(h*r[i]) && is_integer(h*r[j]))
return ret;
return 0;
}
bool mixture(int sw,int sh)
{
int a[4]={0,1,2,3};
int i,j,k,h1,h2,tw,th;
for(i=0;i<24;i++)
{
tw=sw,th=sh;
h1=fit(tw,a[0],a[1]);
h2=fit(tw,a[2],a[3]);
if(h1&&h2&&h1+h2==sh)
return true;
next_permutation(a,a+4);
}
return false;
}
int main()
{
int i,t,w,h,sw,sh;
for(t=1;scanf("%d%d",&sw,&sh),sw+sh;t++)
{
bool found=false;
for(i=0;i<4;i++)
{
scanf("%d%d",&w,&h);
r[i]=w*1.0/h;
}
if(fill(sw,sh))
found=true;
else
{
if(mixture(sw,sh))
found=true;
else
{
for(i=0;i<4;i++)
r[i]=1/r[i];
found=mixture(sh,sw);
}
}
if(found)
printf("Set %d: Yes\n",t);
else
printf("Set %d: No\n",t);
}
return 0;
}

42
HDOJ/1346_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int sum;
while(scanf("%d",&sum)!=EOF)
{
if(sum==-1)
break;
int totle, num=-1;
for(int i=10;i>1;i--)
{
totle=sum;
bool bo=true;
for(int j=1;j<=i;j++)
{
if((totle-1)%i==0)
{
totle=(totle-1)/i*(i-1);
}
else
{
bo=false;
break;
}
}
if(totle%i!=0)
bo=false;
if(bo)
{
num=i;
break;
}
}
if(num!=-1)
printf("%d coconuts, %d people and 1 monkey\n",sum,num);
else
printf("%d coconuts, no solution\n",sum);
}
}

55
HDOJ/1347_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int b[10005];
int main()
{
int n,m,i,j,max1,max2,l;
int a;
while(scanf("%d %d",&n,&m)!=EOF&&n!=0||m!=0)
{
memset(b,0,sizeof(b));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a);
b[a]++;
}
}
max1=-1;max2=-1;
for(i=1;i<=10000;i++)
{
if(b[i]>max1)
{
max2=max1;
max1=b[i];
}
else
{
if(b[i]>max2)
{
max2=b[i];
}
}
}
l=0;
for(i=1;i<=10000;i++)
{
if(b[i]==max2)
{
if(l==0)
{
printf("%d",i);
l++;
}
else
printf(" %d",i);
}
}
printf("\n");
}
return 0;
}

88
HDOJ/1348_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdlib>
const int MAX=1001;
const double PI=3.1415926;
int n;
int top;
using namespace std;
typedef struct Node
{
double x;
double y;
};
Node s[MAX],stack[MAX];
double Distance(Node a,Node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Mul(Node p2, Node p1, Node p0)
{
return (p2.x - p0.x) * (p1.y - p0.y) - (p1.x - p0.x) * (p2.y - p0.y);
}
bool cmp(Node p2,Node p1)
{
double m;
m=Mul(p2,p1,s[0]);
if(m>0||(!m&&Distance(s[0],p2)<Distance(s[0],p1)))
return true;
return false;
}
void convex_hull()
{
int i;
for(i=1;i<n;i++)
{
Node temp;
if(s[i].y<s[0].y||(s[i].y==s[0].y&&s[i].x<s[0].x))
{
temp=s[0];
s[0]=s[i];
s[i]=temp;
}
}
sort(s+1,s+n,cmp);
top=1;
stack[0]=s[0];
stack[1]=s[1];
for(i=2;i<n;i++)
{
while(top>=1&&Mul(s[i],stack[top],stack[top-1])>=0)
top--;
top++;
stack[top]=s[i];
}
}
int main()
{
int i,p=0,t,l;
double sum;
while(cin>>t)
{
while(t--)
{
cin>>n>>l;
cout.setf(ios::fixed);
cout.precision(0);
for(i=0;i<n;i++)
cin>>s[i].x>>s[i].y;
convex_hull();
sum=0;
for(i=0;i<top;i++)
{
sum+=Distance(stack[i],stack[i+1]);
}
sum+=Distance(stack[0],stack[top]);
sum+=2*double(l)*PI;
cout<<sum<<endl;
if(t)
cout<<endl;
}
}
return 0;
}

90
HDOJ/1350_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
#define maxn 505
struct Elem
{
int x1, y1, x2, y2;
int s, e;
} ride[maxn];
int n;
int uN, vN;
bool g[maxn][maxn];
int xM[maxn], yM[maxn];
bool chk[maxn];
int cal(int h, int m)
{
return h * 60 + m;
}
int dist(int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
void input()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int h, m;
scanf("%d:%d", &h, &m);
ride[i].s = cal(h, m);
scanf("%d%d%d%d", &ride[i].x1, &ride[i].y1, &ride[i].x2, &ride[i].y2);
ride[i].e = ride[i].s + dist(ride[i].x1, ride[i].y1, ride[i].x2, ride[i].y2);
}
}
bool ok(Elem &a, Elem &b)
{
return dist(a.x2, a.y2, b.x1, b.y1) + a.e < b.s;
}
void make()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = ok(ride[i], ride[j]);
}
bool SearchPath(int u)
{
int v;
for (v = 0; v < vN; v++)
if (g[u][v] && !chk[v])
{
chk[v] = true;
if (yM[v] == -1 || SearchPath(yM[v]))
{
yM[v] = u;
xM[u] = v;
return true;
}
}
return false;
}
int MaxMatch()
{
int u, ret = 0;
memset(xM, -1, sizeof(xM));
memset(yM, -1, sizeof(yM));
for (u = 0; u < uN; u++)
if (xM[u] == -1)
{
memset(chk, false, sizeof(chk));
if (SearchPath(u))
ret++;
}
return ret;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
input();
make();
uN = vN = n;
printf("%d\n", n - MaxMatch());
}
return 0;
}

27
HDOJ/1351_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
int num[10],ans,C;
void cal(int p, int s)
{
if(p==32) return;
int sum=s,i;
for(i=0;i<8;i++) sum+=(num[i]>>p)&1;
if((sum&1)!=((num[8]>>p)&1))
{
ans|=1<<p;
sum=s;
for(i=0;i<8;i++) sum+=((num[i]>>p)&1)^1;
}
cal(p+1,sum/2);
}
int main()
{
for(scanf("%d",&C);C--;)
{
for(int i=0;i<9;i++) scanf("%x",&num[i]);
ans=0;
cal(0,0);
printf("%x\n",ans);
}
return 0;
}

112
HDOJ/1352_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#define eps 1e-8
using namespace std;
struct Point
{
double x,y;
Point(){}
Point(double x0,double y0):x(x0),y(y0){}
void Input()
{
scanf("%lf%lf",&x,&y);
}
};
struct Line
{
Point a,b;
double inter,k;
Line(){}
Line(Point a0,Point b0):a(a0),b(b0){}
};
double Xmult(Point o,Point a,Point b)
{
return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
int Sig(double a)
{
return a<-eps?-1:a>eps;
}
int cmp(Line l1,Line l2)
{
if(!Sig(l1.k-l2.k) && !Sig(l1.inter-l2.inter) && !Sig(l1.a.x-l2.a.x))
return l1.b.x<l2.b.x;
if(!Sig(l1.k-l2.k) && !Sig(l1.inter-l2.inter))
return l1.a.x<l2.a.x;
if(!Sig(l1.k-l2.k))
return l1.inter<l2.inter;
return l1.k<l2.k;
}
int Count(vector<Line> v)
{
int n=v.size();
for(int i=0;i<n;i++)
{
v[i].inter=-(v[i].a.x)*(v[i].b.y-v[i].a.y)/(v[i].b.x-v[i].a.x)+v[i].a.y;
v[i].k=(v[i].b.y-v[i].a.y)/(v[i].b.x-v[i].a.x);
}
sort(v.begin(),v.end(),cmp);
int ans=0;
for(int i=1;i<n;i++)
{
if(!Sig(v[i].inter-v[i-1].inter) && !Sig(v[i].k-v[i-1].k) && Sig(v[i].a.x-v[i-1].b.x)<=0)
{
ans++;
v[i].b.x=max(v[i-1].b.x,v[i].b.x);
}
}
return ans;
}
int cmp1(Line l1,Line l2)
{
if(!Sig(l1.inter-l2.inter) && !Sig(l1.a.y-l2.a.y))
return l1.b.y<l2.b.y;
if(!Sig(l1.inter-l2.inter))
return l1.a.y<l2.a.y;
return l1.inter<l2.inter;
}
int main()
{
int n;
Point a,b;
vector<Line> v;
vector<Line> v1;
while(scanf("%d",&n) && n)
{
v.clear();
v1.clear();
for(int i=0;i<n;i++)
{
a.Input();
b.Input();
if(a.x>b.x)
swap(a,b);
if(a.x==b.x)
v1.push_back(Line(a,b));
else
v.push_back(Line(a,b));
}
int ans=0;
ans=Count(v);
for(int i=0;i<v1.size();i++)
{
v1[i].inter=v1[i].a.x;
if(v1[i].a.y>v1[i].b.y)
swap(v1[i].a,v1[i].b);
}
sort(v1.begin(),v1.end(),cmp1);
for(int i=1;i<v1.size();i++)
if(!Sig(v1[i].inter-v1[i-1].inter) && Sig(v1[i].a.y-v1[i-1].b.y)<=0)
{
ans++;
v1[i].b.y=max(v1[i].b.y,v1[i-1].b.y);
}
printf("%d\n",n-ans);
}
return 0;
}

63
HDOJ/1353_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[700];
int pr[700];
int main()
{
int a,b[5],c[5]={25,10,5,1},op[50];
char s[100];
op[25]=0;op[10]=1;op[5]=2;op[1]=3;
while(scanf(" %s %d %d %d %d",s,&b[0],&b[1],&b[2],&b[3])!=EOF)
{
memset(dp,-1,sizeof(dp));
dp[0]=0;
bool flag=false;
int pos;
for(int i=0;s[i]!='\0';i++)
if(s[i]=='.')
{
pos=i;
flag=true;
break;
}
if(flag)
{
int x,y;
sscanf(s,"%d.%d",&x,&y);
if(y<10&&s[pos+1]!='0')
y*=10;
a=x*100+y;
}
else
sscanf(s,"%d",&a);
for(int i=0;i<4;i++)
{
int v=c[i],n=b[i];
if(n*v>=a)
for(int i=0;i+v<=a;i++)
{
if(dp[i]!=-1&&(dp[i+v]==-1||dp[i+v]>dp[i]+1))
dp[i+v]=dp[i]+1,pr[i+v]=i;
}
else
{
while(n--)
for(int i=a;i-v>=0;i--)
if(dp[i-v]!=-1&&(dp[i]==-1||dp[i]>dp[i-v]+1))
dp[i]=dp[i-v]+1,pr[i]=i-v;
}
}
if(dp[a]==-1)
puts("NO EXACT CHANGE");
else
{
int tot[4];
memset(tot,0,sizeof(tot));
for(int i=a;i!=0;tot[op[i-pr[i]]]++,i=pr[i]);
printf("%d %d %d %d\n",tot[0],tot[1],tot[2],tot[3]);
}
}
return 0;
}

103
HDOJ/1354_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
typedef struct
{
int next1,next2;
char type;
string text;
string end;
}Page;
Page page[101];
int mark[101];
int path[101];
void init()
{
for(int i = 0;i < 101; i++)
{
page[i].end = "";
page[i].text = "";
page[i].next1 = 0;
page[i].next2 = 0;
mark[i] = 0;
path[i] = 0;
}
}
bool dfs(int p,int k)
{
if(mark[p]==1)
return false;
path[k] = p;
mark[p] = 1;
if(page[p].type=='E'&&page[p].end.compare("HAPPY")==0)
return true;
if(page[p].type == 'C')
{
if(dfs(page[p].next1,k+1))
return true;
if(dfs(page[p].next2,k+1))
return true;
}
path[k] = 0;
mark[p] = 0;
return false;
}
void print()
{
int i = 1;
while(path[i]!=0)
{
cout<<page[path[i]].text<<endl;
i++;
}
}
int main()
{
int n;
int x;
int page1,page2;
string text,end;
string line;
cin>>n;
for(int m = 1; m <= n; m++)
{
cin>>x;
getchar();
init();
for(int i = 1; i <= x; i++)
{
getline(cin,line);
if(line[0] == 'C')
{
page1 = atoi(line.substr(line.find_last_of("\"")+2,1).c_str());
page2 = atoi(line.substr(line.find_last_of("\"")+4,1).c_str());
text = line.substr(line.find_first_of("\"")+1,line.find_last_of("\"")-line.find_first_of("\"")-1);
page[i].next1 = page1;
page[i].next2 = page2;
page[i].text = text;
page[i].type = 'C';
}else if(line[0] == 'E')
{
text = line.substr(line.find_first_of("\"")+1,line.find_last_of("\"")-line.find_first_of("\"")-1);
end = line.substr(line.find_last_of("\"")+2,line.length()-line.find_last_of("\""));
page[i].end = end;
page[i].text = text;
page[i].type = 'E';
}
line = "";
cin.clear();
}
mark[1] = 1;
path[1] = 1;
cout<<"STORY "<<m<<endl;
if(dfs(page[1].next1,2))
print();
else
{
if(dfs(page[1].next2,2))
print();
}
}
return 0;
}

53
HDOJ/1355_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int abss(int x)
{
if(x>0)
return x;
return -x;
}
struct aa
{
int x,y,c;
}a[5000];
bool camp(aa x,aa y)
{
return x.c>y.c;
}
int main ()
{
int xx,yy,i,j,sum,maxt,t,q,w,ss;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&xx,&yy,&maxt);
for(w=i=1;i<=xx;i++)
for(j=1;j<=yy;j++)
{
scanf("%d",&q);
a[w].x=i;
a[w].y=j;
a[w].c=q;
w++;
}
sort(a+1,a+w,camp);
for(ss=sum=0,i=1;i<w;i++)
{
if(i==1)
sum+=a[1].x;
else
sum+=(abss(a[i].x-a[i-1].x)+abss(a[i].y-a[i-1].y));
if(sum+a[i].x<=maxt-1)
{
ss+=a[i].c;
sum++;
}
else
break;
}
printf("%d\n",ss);
}
return 0;
}

5
HDOJ/1356_autoAC.cpp Normal file
View File

@ -0,0 +1,5 @@
1. You can measure dmg using x many amg weights and y many bmg weights.
2. The total number of weights (x + y) is the smallest among those pairs of nonnegative
integers satisfying the previous condition.
3. The total mass of weights (ax + by) is the smallest among those pairs of nonnegative
integers satisfying the previous two conditions.

43
HDOJ/1358_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<stdio.h>
#include<string.h>
int next[1000005],n;
char s[1000005];
void getnext(char *s)
{
int i,j;
memset(next,0,sizeof(next));
next[0]=-1;
i=0;
j=-1;
while(i<n)
{
if(j==-1||s[i]==s[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
int c=0;
while(scanf("%d",&n)!=EOF,n)
{
int i;
scanf("%s",s);
getnext(s);
printf("Test case #%d\n",++c);
for(i=2;i<=n;i++)
{
int len=i-next[i];
if(i%len==0&&i/len>1)
{
printf("%d %d\n",i,i/len);
}
}
printf("\n");
}
}

3
HDOJ/1361_autoAC.cpp Normal file
View File

@ -0,0 +1,3 @@
S(((()()())))
P-sequence 4 5 6666
W-sequence 1 1 1456

130
HDOJ/1362_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int hexagon[110][110];
int ori_size,n,cut_size[11];
bool can_cut(int x,int y,int size)
{
int i,j;
if (y+2*size-2>ori_size*4 || x+size-1>ori_size*2)
return false;
if (y%2==1)
{
for (i=0;i<size;i++)
for (j=0;j<2*i+1;j++)
if (hexagon[x+i][y+j]==false)
return false;
}
else
{
for (i=0;i<size;i++)
for (j=2*i;j<2*size-1;j++)
if (hexagon[x+i][y+j]==false)
return false;
}
return true;
}
void cut(int x,int y,int size)
{
int i,j;
if (y%2==1)
for (i=0;i<size;i++)
for (j=0;j<2*i+1;j++)
hexagon[x+i][y+j]=false;
else
for (i=0;i<size;i++)
for(j=2*i;j<2*size-1;j++)
hexagon[x+i][y+j]=false;
}
void decut(int x,int y,int size)
{
int i,j;
if (y%2==1)
for (i=0;i<size;i++)
for (j=0;j<2*i+1;j++)
hexagon[x+i][y+j]=true;
else
for (i=0;i<size;i++)
for(j=2*i;j<2*size-1;j++)
hexagon[x+i][y+j]=true;
}
bool dfs(int x,int y)
{
int i,j;
if (x>ori_size*2)
return true;
if (y>4*ori_size)
return dfs(x+1,1);
if (hexagon[x][y]==false)
{
for (j=y+1;j<=4*ori_size;j++)
if (hexagon[x][j])
break;
return dfs(x,j);
}
for (i=0;i<n;i++)
{
if (can_cut(x,y,cut_size[i]))
{
cut(x,y,cut_size[i]);
if (dfs(x,y+1))
return true;
decut(x,y,cut_size[i]);
}
else
break;
}
return false;
}
int main(int argc, char** argv)
{
int test_cases,i,j,k;
bool flag;
scanf("%d",&test_cases);
while (test_cases--)
{
scanf("%d%d",&ori_size,&n);
for (i=0,flag=false;i<n;i++)
{
scanf("%d",&cut_size[i]);
if (ori_size%cut_size[i]==0)
flag=true;
}
if (flag)
{
printf("YES\n");
continue;
}
memset(hexagon,false,sizeof(hexagon));
for (i=1;i<=ori_size;i++)
for (j=1;j<=ori_size*2-1+2*i;j++)
hexagon[i][j]=true;
for (i=ori_size+1;i<=ori_size*2;i++)
for (j=(i-ori_size)*2;j<=ori_size*4;j++)
hexagon[i][j]=true;
sort(cut_size,cut_size+n);
for (i=0;i<n;i++)
if (cut_size[i]>ori_size)
{
n=i-1;
break;
}
for (i=0;i<n;i++)
for (j=0;j<i;j++)
if (cut_size[i]%cut_size[j]==0)
{
for (k=i;k<n-1;k++)
cut_size[k]=cut_size[k+1];
i--;
n--;
break;
}
if (dfs(1,1))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

66
HDOJ/1364_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mx=111;
int mp[mx][mx],T,n,m,num,ans;
bool used[mx][mx];
int dx[]={0,0,1,-1},dy[]={-1,1,0,0};
struct MOVE
{
int least,most,dir;
}mov[1000];
bool dfs(int x,int y,int co,int step)
{
if(step==num)
{
return true;
}
if(co>=mov[step].least&&co<=mov[step].most)
{
if(dfs(x,y,0,step+1))
return true;
}
if(co<mov[step].most)
{
int nx=x+dx[mov[step].dir],ny=y+dy[mov[step].dir];
if(nx>=0&&nx<n&&ny>=0&&ny<m&&mp[nx][ny]==0)
if(dfs(nx,ny,co+1,step))
return true;
}
return false;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&mp[i][j]);
num=0;
while(scanf("%d%d",&mov[num].least,&mov[num].most),mov[num].least||mov[num].most)
{
char c;
cin>>c;
if(c=='R')mov[num++].dir=1;
else if(c=='L') mov[num++].dir=0;
else if(c=='U') mov[num++].dir=3;
else mov[num++].dir=2;
}
ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i][j]==0)
{
if(dfs(i,j,0,0))ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}

153
HDOJ/1365_autoAC.cpp Normal file
View File

@ -0,0 +1,153 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
int right,child;
char ch;
bool end;
}tree[350010];
int index = 1,cnt = 0,mat[100],tl[100];
char answer[128],key[125],list[100][30];
bool used[128],findans,matched[100];
void insert(int p,char *word){
if (tree[p].ch){
if (tree[p].ch == *word){
if (!*(word + 1)) tree[p].end = 1;
else{
if(!tree[p].child) tree[p].child = ++index;
insert(tree[p].child,word + 1);
}
}
else{
if (tree[p].right) insert(tree[p].right,word);
else {
tree[p].right = ++ index;
insert(tree[p].right,word);
}
}
}
else{
tree[p].ch = *word;
if (!*(word + 1)) tree[p].end = 1;
else{
tree[p].child = ++ index;
insert(tree[p].child,word + 1);
}
}
}
int findwordmat(){
for (int i = 0;i < cnt;i ++){
mat[i] = 0;
for (int j = 0;j < tl[i];j ++)
if (key[list[i][j]])
++ mat[i];
}
}
int subsearch(int cur,int wi,int num,int tnum);
int search(int cur){
if (cur == cnt){
if (findans) return 2;
findans = 1;
for (int i = 'A';i <= 'Z';i ++) answer[key[i]] = i;
return 1;
}
findwordmat();
int m = -1,wi;
for (int i = 0;i < cnt;i ++)
if (!matched[i] && mat[i] > m){
m = mat[i];
wi = i;
}
return subsearch(cur,wi,0,1);
}
int subsearch(int cur,int wi,int num,int tnum){
if (num == tl[wi]){
matched[wi] = true;
int tmp = search(cur + 1);
matched[wi] = false;
return tmp;
}
if (!tnum) return 0;
char cc = list[wi][num];
if (key[cc]){
for (;tnum;tnum = tree[tnum].right)
if (tree[tnum].ch == key[cc]){
if (num + 1 == tl[wi] && !tree[tnum].end) return 0;
return subsearch(cur,wi,num + 1,tree[tnum].child);
}
return 0;
}else{
int find = 0;
for (;tnum;tnum = tree[tnum].right)
if (!used[tree[tnum].ch]){
if (num + 1 == tl[wi] && !tree[tnum].end) continue;
used[tree[tnum].ch] = true;
key[cc] = tree[tnum].ch;
int tmp = subsearch(cur,wi,num + 1,tree[tnum].child);
key[cc] = 0;
used[tree[tnum].ch] = false;
if (tmp == 2) return tmp;
if (tmp == 1) find = tmp;
}
return find;
}
}
int main(){
char word[130];
int w,t,tmp,pd,T;
scanf("%d",&w);
for (int i = 1;i <= w;i ++){
scanf("%s",word);
insert(1,word);
}
scanf("%d",&T);
while (T --){
memset(list,0,sizeof list);
cnt = 0,tmp = 0,pd = 0;
findans = 0;
memset(key,0,sizeof key);
memset(answer,0,sizeof answer);
char c;
while (c = getchar(),c < 'A' || c > 'z');
for (;;){
if (c >= 'A' && c <= 'Z'){
pd = false; list[cnt][tmp ++] = c;
}
else if (c == ' '){
pd = false;
if (tmp){
tl[cnt] = tmp;list[cnt ++][tmp] = 0;
tmp = 0;
}
}
else if (c == '\n'){
if (tmp){
tl[cnt] = tmp;list[cnt ++][tmp] = 0;
tmp = 0;
}
if (pd) break;
pd = 1;
}
if (scanf("%c",&c) == EOF){
if (tmp){
tl[cnt] = tmp;list[cnt ++][tmp] = 0;
tmp = 0;break;
}
}
}
int tmp = search(0);
if (tmp == 0) puts("#No solution#");
else if (tmp == 1){
for (int i = 'A';i <= 'Z';i ++){
if (answer[i]) printf("%c",answer[i]);
else printf("*");
}
printf("\n");
}else{
puts("#More than one solution#");
}
}
return 0;
}

96
HDOJ/1366_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#define N 1000000
using namespace std;
struct ty
{
int x,y,len,bj;
int linkx[75];
int linky[75];
}pipe[50];
struct ty2
{
int v,bj,link;
}f[120];
int ans;
int ok()
{
int i,j,ii,flag[50],k;
ans=0;
memset(flag,0,sizeof(flag));
flag[1]=1;
for(i=0;i<=110;i++)f[i].link=-1;
for(i=0;i<=110;i++)
{
if((pipe[1].y<=i)&&(i<pipe[1].y+pipe[1].len))f[i].v+=1;
if(i<pipe[1].y)f[i].v+=10000000;
}
for(j=1;j<=pipe[1].linky[0];j++)
f[pipe[1].linky[j]].link=pipe[1].linkx[j];
if(pipe[1].bj!=-1)f[pipe[1].bj].bj=1;
while(1)
{
for(i=110;i>=0;i--)
{
if(f[i].v>=10000000)return f[i+2].bj;
if(f[i+1].bj==1)return 1;
ans+=f[i].v;
f[i].v=0;
if((f[i].link>=0)&&(flag[f[i].link]==0))
{
k=f[i].link;
f[i].link=-1;
flag[k]=1;
for(j=0;j<=110;j++)
{
if((pipe[k].y<=j)&&(j<pipe[k].y+pipe[k].len))f[j].v+=1;
if(j<pipe[k].y)f[j].v+=1000000000;
}
for(j=1;j<=pipe[k].linky[0];j++)
f[pipe[k].linky[j]].link=pipe[k].linkx[j];
if(pipe[k].bj!=-1)f[pipe[k].bj].bj=1;
break;
}
}
if(i==-1)return 0;
}
}
int main()
{
int t,p,l,x,y,len,t1,t2;
scanf("%d",&t);
while(t--)
{
memset(pipe,0,sizeof(pipe));
memset(f,0,sizeof(f));
scanf("%d",&p);
for(int i=1;i<=p;i++)
{
scanf("%d%d%d",&pipe[i].x,&pipe[i].y,&pipe[i].len);
pipe[i].bj=-1;
}
scanf("%d",&l);
for(int i=1;i<=l;i++)
{
scanf("%d%d%d",&x,&y,&len);
for(int j=1;j<=p;j++)
{
if(pipe[j].x+1==x)t1=j;
if(pipe[j].x==x+len)t2=j;
}
pipe[t1].linkx[++pipe[t1].linkx[0]]=t2;
pipe[t1].linky[++pipe[t1].linky[0]]=y;
pipe[t2].linkx[++pipe[t2].linkx[0]]=t1;
pipe[t2].linky[++pipe[t2].linky[0]]=y;
}
scanf("%d%d",&x,&y);
pipe[x].bj=y;
if(y<pipe[x].y||y>pipe[x].y+pipe[x].len){printf("No Solution\n");continue;}
if(ok())printf("%d\n",ans);
else printf("No Solution\n");
}
return 0;
}

103
HDOJ/1369_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<iostream>
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
struct Editor{
int ID;
int Num;
};
vector<int>data[10000];
map<string,int> name;
map<string,int>::iterator pos;
int author[10000];
string Erdos = "Erdos,P.";
void bfs()
{
queue<Editor>Q;
Editor tmp,now;
int i,id,nNum;
memset(author,-1,sizeof(author));
tmp.ID = name[Erdos];
tmp.Num = 0;
Q.push(tmp);
while(!Q.empty())
{
now = Q.front(); Q.pop();
id = now.ID;
nNum = data[id].size();
for(i=0;i<nNum;i++)
{
if(author[data[id][i]] == -1)
{
author[data[id][i]] = now.Num + 1;
tmp.ID = data[id][i];
tmp.Num = now.Num + 1;
Q.push(tmp);
}
}
}
}
int main()
{
char str[300];
string fullname;
int p,n,len,numAuthor,numPaper,i,j,title;
int paper[300],iCase = 0;
while(scanf("%d%d",&p,&n)!=EOF && p+n)
{
for(i=0;i<10000;i++)
data[i].clear();
name.clear();
numAuthor = 0;
while(p--)
{
numPaper = 0;
int flag = 1;
while(1)
{
scanf("%s",str);
fullname = str;
scanf("%s",str); len = strlen(str);
if(str[len-1] == ':' || str[len-1] == '.') flag = 0;
str[len-1] = '\0';
fullname += str;
pos = name.find(fullname);
if(pos == name.end())
name[fullname] = numAuthor ++;
paper[numPaper++] = name[fullname];
if(!flag)
{
gets(str); break;
}
}
for(i=0;i<numPaper;i++)
{
for(j=0;j<numPaper;j++)
{
if(i!=j)
data[paper[i]].push_back(paper[j]);
}
}
}
pos = name.find(Erdos);
if(pos == name.end()) name[Erdos] = numAuthor++;
bfs();
printf("Database #%d\n",++iCase);
while(n--)
{
scanf("%s",str); printf("%s",str);
fullname = str;
scanf("%s",str); printf(" %s: ",str);
fullname +=str;
pos = name.find(fullname);
if(pos == name.end() || author[name[fullname]]==-1)
printf("infinity\n");
else printf("%d\n",author[name[fullname]]);
}
printf("\n");
}
return 0;
}

24
HDOJ/1370_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main()
{ int p,e,i,d,n,T;
int j,k;
scanf("%d",&T);
while(T--)
{ j=1;
getchar();
while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF)
{
if(p==-1&&e==-1&&i==-1&&d==-1)
break;
else
{ n=( 5544*p+14421*e+1288*i-d+21252)%21252;
if(n==0)
n=21252;
printf("Case %d: the next triple peak occurs in %d days.\n",j++,n);
}
}
if(T!=0)
printf("\n");
}
return 0;
}

61
HDOJ/1371_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<cstdio>
#include<vector>
using namespace std;
namespace
{
vector<pair<pair<int, int>, pair<int, int> > > V;
void init()
{
V.push_back(make_pair(make_pair(1, 5), make_pair(1, 6)));
V.push_back(make_pair(make_pair(2, 10), make_pair(2, 11)));
V.push_back(make_pair(make_pair(3, 16), make_pair(3, 17)));
V.push_back(make_pair(make_pair(4, 21), make_pair(4, 22)));
V.push_back(make_pair(make_pair(5, 27), make_pair(5, 28)));
V.push_back(make_pair(make_pair(6, 32), make_pair(6, 33)));
V.push_back(make_pair(make_pair(7, 38), make_pair(7, 39)));
V.push_back(make_pair(make_pair(8, 43), make_pair(8, 44)));
V.push_back(make_pair(make_pair(9, 48), make_pair(9, 49)));
V.push_back(make_pair(make_pair(10, 54), make_pair(10, 55)));
}
bool before(int h1, int m1, int h2, int m2)
{
if (h1 != h2)
return h1 < h2;
else
return m1 <= m2;
}
int pass(int bh, int bm, int eh, int em)
{
int total = 0;
for (size_ i = 0; i < V.size(); i++)
{
pair<int, int> &begin = V[i].first;
pair<int, int> &end = V[i].second;
if (before(bh, bm, begin.first, begin.second)
&& before(end.first, end.second, eh, em))
total++;
}
return total;
}
}
int main()
{
init();
int bh, bm, eh, em;
puts("Program 3 by team X");
puts("Initial time Final time Passes");
while (scanf("%d%d%d%d", &bh, &bm, &eh, &em) != EOF)
{
bh %= 12;
eh %= 12;
int res;
if (bh > eh || (bh == eh && bm > em))
res = pass(bh, bm, 12, 0) + pass(0, 0, eh, em) + 1;
else
res = pass(bh, bm, eh, em);
printf(" %02d:%02d %02d:%02d%8d\n", bh ? bh : 12, bm,
eh ? eh : 12, em, res);
}
puts("End of program 3 by team X");
return 0;
}

25
HDOJ/1372_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <string.h>
int knight[8][8];
int x[]={1,1,2,2,-1,-1,-2,-2};
int y[]={2,-2,1,-1,2,-2,1,-1};
void dfs(int si,int sj,int moves)
{
if(si < 0 || sj < 0 || si >= 8 || sj >= 8 || moves >= knight[si][sj])
return;
knight[si][sj] = moves;
int i;
for(i = 0; i < 8; i++)
dfs(si + x[i],sj + y[i],moves + 1);
}
int main()
{
char a[10],b[10];
while(scanf("%s%s",a,b) != EOF)
{
memset(knight,10,sizeof(knight));
dfs(a[0] - 'a',a[1] - '1',0);
printf("To get from %s to %s takes %d knight moves.\n",a,b,knight[b[0] - 'a'][b[1] - '1']);
}
return 0;
}

123
HDOJ/1373_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define debug 0
#define sca scanf
#define pri printf
#define LL long long
#define fou(i,a,b) for(i = a; i < b; i ++)
#define fod(i,a,b) for(i = a; i >= b; i --)
#define clean(a) memset(a,0,sizeof(a))
using namespace std;
int Compare(int a,int b,bool flag)
{if(flag)return a>b?a:b; else return a<b?a:b;}
int ABS(int a,int b)
{return (a)>(b)?(a-b):(b-a);}
int Map[30][30];
int channel[30];
int mark[30][30];
int n,ans,key,res;
void dfs(int x)
{
if(debug)pri("%d\n",x);
int i,j,u,v;
if(key)
{
return ;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(Map[x][j]&&mark[j][i])
{
break;
}
}
if(j==n+1)
{
mark[x][i]=1;
channel[i]=1;
if(debug)
{
pri("X:%d I:%d J:%d\n",x,i,j);
pri("mark-----------\n");
fou(i,1,n + 1)
{
pri("%d ",mark[x][i]);
}
pri("\nchannel:\n");
fou(i,1,n + 1)
{
pri("%d ",channel[i]);
}
pri("\n");
}
if(x == n)
{
for(u=1;u<=n;u++)
{
if(channel[u])
{
res++;
}
}
key=1;
return ;
}else
{
dfs(x+1);
}
mark[x][i]=0;
channel[i]=0;
}
}
}
int main()
{
int i,j,k,len;
char tmp[100];
while(~sca("%d",&n) && n)
{
clean(mark);
clean(channel);
clean(Map);
fou(i,1,n + 1)
{
sca("%s",tmp);
len = strlen(tmp);
fou(j,2,len)
{
Map[i][tmp[j] - 'A' + 1] = 1;
Map[tmp[j] - 'A' + 1][i] = 1;
}
}
res = 0; key = 0;
dfs(1);
if(res ==1 )
pri("%d channel needed.\n",res);
else
pri("%d channels needed.\n",res);
if(debug)
{
fou(i,1,n + 1)
pri("%d ",channel[i]);
pri("\n");
fou(i,1,n + 1)
pri("%d ",mark[i]);
pri("\n");
fou(i,1,n + 1)
{
fou(j,1,n + 1)
{
pri("%d ",Map[i][j]);
}
pri("\n");
}
}
}
return 0;
}

21
HDOJ/1374_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
double square(double x)
{
return x * x;
}
int main(void)
{
double x1,y1,x2,y2,x3,y3;
double a2,b2,c2;
double r;
while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3) != EOF)
{
a2 = square(x1 - x2) + square(y1 - y2);
b2 = square(x1 - x3) + square(y1 - y3);
c2 = square(x3 - x2) + square(y3 - y2);
r = sqrt(c2/(1 - square(a2 + b2 - c2)/a2/b2/4))/2;
printf("%.2f\n",2 * PI * r);
}
}

139
HDOJ/1375_autoAC.cpp Normal file
View File

@ -0,0 +1,139 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct P
{
char s[5];
int n;
}node[11000], get[11000];
int cmp(const void *a , const void *b)
{
return strcmp( (*(P *)a).s, (*(P *)b).s );
}
char str[11000], str1[11000];
int n, num;
void element(char *str, int s, int t, int k, P *p);
void insert(char *s, int k, P *p)
{
int i;
for ( i = 0 ; i < num ; i++ )
{
if ( !strcmp(s, p[i].s) )
break;
}
if ( i < num )
p[i].n+=k;
else p[num].n+=k, strcpy(p[num++].s, s);
}
void sequence(char *str, int s, int t, int k, P *p)
{
int i = s;
while ( i < t )
{
if ( '(' ==str[i] )
{
int ti, tk;
for ( ti = i+1, tk= 1 ; tk ; ti++ )
{
if ( '(' == str[ti] )
tk++;
else if ( ')' == str[ti] )
tk--;
}
if ( '0' <= str[ti] && str[ti] <= '9' )
{
int ttk, te= ti;
for ( ttk = 0; '0' <= str[ti] && str[ti] <= '9' ; ti++ )
ttk= ttk*10+str[ti]-'0';
element(str, i, te, k*ttk, p);
}
else element(str, i, ti, k, p);
i= ti;
}
else if ( 'A' <= str[i] && str[i] <= 'Z' )
{
int tk= i;
i++;
if ( 'a' <= str[i] && str[i] <= 'z' )
i++;
if ( '0' <= str[i] && str[i] <= '9' )
{
int ttk;
for ( ttk = 0; '0' <= str[i] && str[i] <= '9' ; i++ )
ttk= ttk*10+str[i]-'0';
element(str, tk, i, k*ttk, p);
}
else element(str, tk, i, k, p);
}
}
}
void element(char *str, int s, int t, int k, P *p)
{
if ( '(' == str[s] && ')' == str[t-1] )
sequence(str, s+1, t-1, k, p);
else
{
char tmp[5];
tmp[0] = str[s];
if ( 'a' <= str[s+1] && str[s+1] <= 'z' )
tmp[1]= str[s+1], tmp[2]= 0;
else tmp[1]= 0;
insert(tmp, k, p);
}
}
void slove(char *str, P *p)
{
int len = strlen(str), i= 0, j, k;
num= 0;
while ( i < len )
{
char ch;
ch= str[i];
if ( '0' <= ch && ch <= '9' )
{
for ( k = 0; '0' <= str[i] && str[i] <= '9' ; i++ )
k= k*10+str[i]-'0';
for ( j = i ; str[j] && '+' != str[j] ; j++ );
sequence(str, i, j, k, p);
i= j+1;
}
else
{
for ( j = i+1 ; str[j] && '+' != str[j] ; j++ );
sequence(str, i, j, 1, p);
i= j+1;
}
}
}
int main()
{
while ( EOF!= scanf("%s", str) )
{
memset(node, 0, sizeof(node));
slove(str, node);
int m= num;
qsort(node, m, sizeof(P), cmp);
scanf("%d", &n);
while ( n-- )
{
scanf("%s", str1);
memset(get, 0, sizeof(get));
slove(str1, get);
if ( m == num )
{
qsort(get, m, sizeof(P), cmp);
int i;
for ( i = 0 ; i <= num && node[i].n == get[i].n && !strcmp(node[i].s, get[i].s) ; i++ );
if ( i > num )
{
printf("%s==%s\n", str, str1);
continue;
}
}
printf("%s!=%s\n", str, str1);
}
}
return 0;
}

29
HDOJ/1376_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <string.h>
const int MAXN = 100;
int main(void)
{
char str[MAXN];
int i,j;
while(scanf("%s",str) != EOF)
{
char str1[MAXN];
memset(str1,0x30,sizeof(str1));
int index = 0;
for(i = strlen(str) - 1; i > 1; i--)
{
int num = str[i] - '0';
int temp;
for(j = 0; j < index || num; j++)
{
temp = num * 10 + (j < index ? str1[j] - '0': 0);
str1[j] = temp/8 + '0';
num = temp%8;
}
index = j;
}
str1[j] = '\0';
printf("%s [8] = 0.%s [10]\n",str,str1);
}
return 0;
}

145
HDOJ/1377_autoAC.cpp Normal file
View File

@ -0,0 +1,145 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
#define ERROR non=true;return
set<string> used;
stack<string> s;
bool non, root;
bool gettag(char* &ch, string &tag, bool &end, bool &empty)
{
int p1, p2, p3, p4;
string attribute;
end=false, empty=false;
tag=attribute="";
ch++;
if (*ch=='/'){end=true;ch++;}
while (isalnum(*ch) || *ch=='-')
{
tag+=*ch;
ch++;
}
while (isspace(*ch)) ch++;
if (*ch=='>')
return 1;
if (*ch=='/')
{
int cal=0;
ch++;
while (*ch!='>'){cal++;ch++;}
if (cal>0) return -2;
if (end) return -1;
else empty=true;
}
while (isalnum(*ch) || *ch=='-' || *ch=='.' || *ch=='\"' || *ch=='=')
{
attribute+=*ch;
ch++;
}
if (attribute.length()>0)
{
p1=attribute.find("=", 0); if (p1==-1) return -3;
p2=attribute.find("\"", 0); if (p2<p1) return -4;
p2=attribute.find("\"", p1+1); if (p2==-1) return -5;
p3=attribute.find("\"", p2+1); if (p3==-1 || p3==p2+1) return -6;
p4=attribute.find("\"", p3+1); if (p4!=-1) return -7;
p4=attribute.find("=", p1+1); if (p4!=-1) return -8;
}
while (isspace(*ch)) ch++;
if (*ch=='>') return 1;
if (*ch=='/')
{
int cal=0;
ch++;
while (*ch!='>'){cal++;ch++;}
if (cal>0) return -10;
if (end) return -9;
else empty=true;
}
while (*ch!='>'){
ch++;
if (!isspace(*ch) && *ch!='>') return -11;
}
return 1;
}
void solve(char *ch)
{
bool end, empty;
string tag;
int tmp;
while (!non && *ch!='\0')
{
if (*ch=='<')
{
tmp=gettag(ch, tag, end, empty);
if (tmp==1)
{
if (end && empty){
ERROR;
}
if (empty && used.count(tag)>0){
ERROR;
}
else if (!empty && !end)
{
if (used.count(tag)>0){
ERROR;
}
if (s.empty())
{
if (root){ERROR;}
else root=true;
}
used.insert(tag);
s.push(tag);
}
else if (!empty && end)
{
if (s.empty() || s.top()!=tag){
ERROR;
}
used.erase(tag);
s.pop();
}
}
else{ERROR;}
}
else ch++;
}
}
int main()
{
string tag, attribute;
bool firstdata=true;
bool end, empty;
char buf[65536];
do{
while (gets(buf)!=NULL && strcmp(buf, "<span class="crayon-ta"><?xml version=\1.0\?>)!=0
&& strcmp(buf, "<span class="crayon-ta"><?end?>)!=0)
{
solve(buf);
}
if (strcmp(buf, "<span class="crayon-ta"><?xml version=\1.0\?>)==0)
{
if (!firstdata)
{
if (non || !s.empty()) printf("non well-formed\n");
else printf("well-formed\n");
}
else firstdata=false;
non=false;
root=false;
used.clear();
while (!s.empty()) s.pop();
}
}while (strcmp(buf, "<span class="crayon-ta"><?end?>)!=0);
if (non || !s.empty()) printf("non well-formed\n");
else printf("well-formed\n");
return 0;
}

130
HDOJ/1378_autoAC.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int MAXL = 80 + 10;
const int MAXN = 10000 + 10;
char szLine[MAXN];
char szWord[MAXN][MAXL];
int n;
int nWordCnt;
int dp[MAXN];
int nLastTo[MAXN];
int nSum[MAXN];
void Read()
{
getchar();
nWordCnt = 0;
while (gets(szLine))
{
if (szLine[0] == '\0') break;
sscanf(szLine, "%s", szWord[++nWordCnt]);
for (int i = 0; szLine[i] != '\0'; ++i)
{
if (szLine[i] == ' ')
{
while (szLine[i] == ' ')
{
i++;
}
sscanf(szLine + i, "%s", szWord[++nWordCnt]);
}
}
}
}
void Init()
{
nSum[0] = 0;
for (int i = 1; i <= nWordCnt; ++i)
{
nSum[i] = nSum[i - 1] + strlen(szWord[i]);
}
}
int Sum(int i, int j)
{
return nSum[j] - nSum[i - 1];
}
int LeastLen(int i, int j)
{
return Sum(i, j) + j - i;
}
int Badness(int i, int j)
{
if (i == j)
{
return 500;
}
int nBlankCnt = n - Sum(i, j);
int nGapCnt = j - i;
int nGapLen = nBlankCnt / nGapCnt;
int nRemain = nBlankCnt % nGapCnt;
return (nGapLen - 1) * (nGapLen - 1) * (nGapCnt - nRemain) + nGapLen * nGapLen * nRemain;
}
void Dp()
{
memset(dp, 0x3f, sizeof(dp));
dp[nWordCnt + 1] = 0;
for (int i = nWordCnt; i >= 1; --i)
{
for (int j = i; j <= nWordCnt && LeastLen(i, j) <= n; ++j)
{
if (dp[j + 1] + Badness(i, j) <= dp[i])
{
dp[i] = dp[j + 1] + Badness(i, j);
nLastTo[i] = j;
}
}
}
}
void PrintBlank(int nNum)
{
for (int i = 0; i < nNum; ++i)
{
putchar(' ');
}
}
void Output()
{
int nL = 1;
while (true)
{
int nR = nLastTo[nL];
if (nL == nR)
{
puts(szWord[nL]);
}
else
{
int nBlankCnt = n - Sum(nL, nR);
int nGapCnt = nR - nL;
int nGapLen = nBlankCnt / nGapCnt;
int nRemain = nBlankCnt % nGapCnt;
printf("%s", szWord[nL]);
for (int i = 0; i < nGapCnt - nRemain; ++i)
{
PrintBlank(nGapLen);
printf("%s", szWord[nL + 1 + i]);
}
for (int i = 0; i < nRemain; ++i)
{
PrintBlank(nGapLen + 1);
printf("%s", szWord[nR - nRemain + 1 + i]);
}
puts("");
}
if (nR == nWordCnt) break;
nL = nR + 1;
}
puts("");
}
int main()
{
while (scanf("%d", &n) == 1 && n)
{
Read();
Init();
Dp();
Output();
}
return 0;
}

46
HDOJ/1379_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
# include <iostream>
# include <algorithm>
# include <string>
# include <vector>
using namespace std;
bool cmp(string a,string b)
{
int x=0,y=0,i,j;
for(i=0;i<a.size();i++)
{
for(j=i+1;j<a.size();j++)
{
if(a[i]>a[j])
x++;
}
}
for(i=0;i<b.size();i++)
{
for(j=i+1;j<b.size();j++)
{
if(b[i]>b[j])
y++;
}
}
return x!=y?x<y:1;
}
int main()
{
int T,n,m,i;
string s;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
vector<string>w;
for(i=0;i<m;i++)
{
cin>>s;
w.push_back(s);
}
sort(w.begin(),w.end(),cmp);
for(i=0;i<m;i++)
cout<<w[i]<<endl;
}
return 0;
}

61
HDOJ/1380_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 10;
struct state {
double dis;
int r, y, g;
}d[N];
struct can {
int l, r;
void put() {
if (l == r)
printf("%d", l);
else
printf("%d-%d", l, r);
}
}s[6*N];
int n, m;
bool judge (double v) {
v /= 3600;
for (int i = 0; i < n; i++) {
int sum = d[i].r + d[i].y + d[i].g;
double t = d[i].dis / v;
int ti = (int)t;
int k = ti / sum;
t = t - k * sum;
if (t > d[i].g + d[i].y)
return false;
}
return true;
}
int main () {
int cas = 1;
while (scanf("%d", &n) == 1 && n != -1) {
for (int i = 0; i < n; i++)
scanf("%lf%d%d%d", &d[i].dis, &d[i].g, &d[i].y, &d[i].r);
m = 0;
for (int i = 30; i <= 60; i++) {
if (judge(i)) {
if (m == 0 || s[m-1].r + 1 < i) {
s[m].l = s[m].r = i;
m++;
} else {
s[m-1].r++;
}
}
}
printf("Case %d: ", cas++);
if (m) {
for (int i = 0; i < m - 1; i++) {
s[i].put();
printf(", ");
}
s[m-1].put();
printf("\n");
} else
printf("No acceptable speeds.\n");
}
return 0;
}

20
HDOJ/1381_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<iostream>
#include<set>
#include<string>
using namespace std;
int main(){
set<string> Set;
int t,n,nc;
string str;
cin>>t;
while(t--){
cin>>n>>nc>>str;
int size=str.size();
for(int i=0;i<size-n+1;i++){
string tmp(str,i,n);
Set.insert(tmp);
}
cout<<Set.size()<<endl;
}
return 0;
}

62
HDOJ/1382_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 35
int n, m;
int d[maxn];
int dir[2] = {-1, 1};
bool vis[maxn];
void input()
{
char st[5];
scanf("%d", &m);
m--;
for (int i = 0; i < n; i++)
{
scanf("%s", st);
if (st[0] == 'R')
d[i] = 1;
else
d[i] = 0;
}
}
int unify(int a)
{
return (a + n) % n;
}
void work()
{
int s = m;
int t = unify(0 + dir[d[m]]);
if (t == m)
t = unify(t + dir[d[m]]);
int cnt = 1;
int ret = 1;
vis[m] = true;
memset(vis, 0, sizeof(vis));
while (cnt < n)
{
vis[s] = true;
d[s] = 1 - d[s];
ret++;
if (!vis[t])
cnt++;
int temp = unify(s + dir[d[t]]);
if (temp == t)
temp = unify(temp + dir[d[t]]);
s = t;
t = temp;
}
printf("Classmate %d got the ball last after %d tosses.\n", s + 1, ret);
}
int main()
{
while (scanf("%d", &n), n)
{
input();
work();
}
return 0;
}

62
HDOJ/1384_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include"iostream"
#include "queue"
#define inf 0x7FFFFFFF
#define M 500010
using namespace std;
int head[M],minm,maxm,dis[M],edge_sum;
bool inq[M];
struct a{
int end,jie,next;
}edge[M];
void Init(){
edge_sum=0;
memset(head,-1,sizeof(head));
minm=inf;
maxm=-inf;
memset(inq,0,sizeof(inq));
memset(dis,-inf,sizeof(dis));
}
void add_edge(int u,int v, int jie){
edge[edge_sum].end=v;
edge[edge_sum].jie=jie;
edge[edge_sum].next=head[u];
head[u]=edge_sum++;
}
int max(int a,int b){ if(a>b)return a;return b;}
int min(int a,int b){ if(a<b)return a;return b;}
int spfa(){
memset(dis,inf,sizeof(dis));
queue<int> q;
while(!q.empty())q.pop();
q.push(minm), inq[minm]=1, dis[minm]=0;
while( !q.empty()){
int u=q.front(); q.pop(), inq[u]=0;
for(int i=head[u] ; i!=-1; i=edge[i].next )
{
int v=edge[i].end, jie=edge[i].jie;
if(dis[v]<dis[u]+jie)
{
dis[v]=dis[u]+jie;
if( !inq[v] ) inq[v]=!inq[v], q.push(v);
}
}
}
return dis[maxm];
}
int main(){
int n;
while(~scanf("%d",&n)){
Init();
while(n--){
int u,v,jie;
scanf("%d %d %d",&u,&v,&jie);
add_edge(u,v+1,jie);
minm=min(u,minm);
maxm=max(v+1,maxm);
}
for( int i=minm;i<=maxm;i++)
add_edge(i,i+1,0), add_edge(i+1,i,-1);
printf("%d\n",spfa());
}
return 0;
}

61
HDOJ/1385_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
#define maxn 50
#define INF 0xffffff
#define Min(a, b) (a > b ? b : a)
int map[maxn][maxn], n;
int patl[maxn][maxn];
int f[maxn];
void floyd();
int main()
{
while(cin >> n, n)
{
int i, j, a, b, next;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
cin >> map[i][j];
if(map[i][j] == -1)
map[i][j] = INF;
}
for(i=1; i<=n; i++)
cin >> f[i];
floyd();
while(cin >> a >> b, a != -1)
{
next = a;
printf("From %d to %d :\nPath: ", a, b);
while(next != b)
{
printf("%d-->", next);
next = patl[next][b];
}
printf("%d\n", next);
printf("Total cost : %d\n\n", map[a][b]);
}
}
return 0;
}
void floyd()
{
int k, i, j;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
patl[i][j] = j;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
int new_patl = map[i][k] + map[k][j] + f[k];
if(map[i][j] > new_patl)
{
map[i][j] = new_patl;
patl[i][j] = patl[i][k];
}
else if(map[i][j] == new_patl)
patl[i][j] = Min(patl[i][j], patl[i][k]);
}
}

80
HDOJ/1387_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<iostream>
#include<string>
using namespace std;
int belong[1000000];
int pos[1005];
struct node
{
int x;
int next;
}r[200010];
int main()
{
int x,t,i,j,n,No=0;
int front,back,used;
string com;
while(cin>>t&&t)
{
front=-1;
back=-1;
used=-1;
for(i=0;i<t;++i)
{
cin>>n;
for(j=0;j<n;++j)
{
cin>>x;
belong[x]=i;
}
pos[i]=-1;
}
cout<<"Scenario #"<<++No<<endl;
while(cin>>com&&com!="STOP")
{
if(com=="ENQUEUE")
{
used++;
cin>>x;
if(pos[belong[x]]==-1)
{
if(front==-1&&back==-1)
{
front=used;
back=used;
r[used].x=x;
r[used].next=-1;
pos[belong[x]]=used;
}
else
{
r[used].x=x;
r[used].next=-1;
r[back].next=used;
back=used;
pos[belong[x]]=used;
}
}
else
{
if(pos[belong[x]]==back)
back=used;
r[used].x=x;
r[used].next=r[pos[belong[x]]].next;
r[pos[belong[x]]].next=used;
pos[belong[x]]=used;
}
}
else if(com=="DEQUEUE")
{
cout<<r[front].x<<endl;
if(pos[belong[r[front].x]]==front)
pos[belong[r[front].x]]=-1;
front=r[front].next;
if(front==-1)
back=-1;
}
}
cout<<endl;
}
return 0;
}

62
HDOJ/1388_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
struct Tile
{
int x1,y1;
int x2,y2;
int s;
}p[101];
int main()
{
int n,i,j,k;
int t1,t2,t3;
int a,b,sum,m,c1,c2,t;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
t1=t2=t3=0;
sum=0;
scanf("%d%d",&a,&b);
scanf("%d",&m);
t=(a>b? a:b);
for(i=0;i<m;i++)
{
scanf("%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
c1=abs(p[i].x1-p[i].x2);
c2=abs(p[i].y1-p[i].y2);
p[i].s=c1*c2;
sum+=p[i].s;
if(p[i].x1<0||p[i].x2<0||p[i].y1 <0||p[i].y2 <0)
t2=1;
if(p[i].x1>t||p[i].x2>t||p[i].y1>t||p[i].y2>t)
t2=1;
}
if(sum<a*b)
t3=1;
for(i=0;i<m;i++)
{ if(t1)break;
for(j=i+1;j<m;j++)
{
if((p[j].x1>=p[i].x2)||(p[j].x2<=p[i].x1)||(p[j].y1>=p[i].y2)||(p[j].y2<=p[i].y1))
continue;
else
{t1=1;
break;
}
}
}
if(t1)
printf("NONDISJOINT\n");
else if(t2)
printf("NONCONTAINED\n");
else if(t3)
printf("NONCOVERING\n");
else
printf("OK\n");
}
}
return 0;
}

26
HDOJ/1390_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<stdio.h>
int main()
{
int N,n,i,flag1;
__int64 flag;
scanf("%d",&N);
while(N--)
{
scanf("%d",&n);
for(i=0,flag=0,flag1=0;flag<n;i++)
{
flag=1<<i;
if((n&flag)==flag&&flag1==1)
{
printf(" %d",i);
}
if((n&flag)==flag && flag1==0)
{
flag1=1;
printf("%d",i);
}
}
printf("\n");
}
return 0;
}

31
HDOJ/1391_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
int x,y;
cin>>x>>y;
if(x==0&&y==0)
cout<<"0\n";
else if(x==y)
{
if(x%2==1)
cout<<(2*x-1)<<endl;
else
cout<<(2*x)<<endl;
}
else if(x==(y+2))
{
if(x%2==0)
cout<<(2*x-2)<<endl;
else
cout<<(2*x-3)<<endl;
}
else
cout<<"No Number\n";
}
return 0;
}

84
HDOJ/1392_autoAC.cpp Normal file
View File

@ -0,0 +1,84 @@
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
}a[110],stack1[110];
double distan(node a,node b)
{
return (double)sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y)*1.0);
}
double across(node a,node n1,node n2)
{
return (n1.x-a.x)*(n2.y-a.y) - (n1.y-a.y)*(n2.x-a.x);
}
bool cmp(node a1,node b1)
{
double k=across(a[0],a1,b1);
if(k>0) return true;
else if (k==0&&distan(a[0],a1)<distan(a[0],b1))
{
return true;
}
else return false;
}
void anglesort(int n)
{
int i,k=0;
node temp;
for(i=1;i<n;i++)
{
if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x))
k=i;
}
temp=a[0];
a[0]=a[k];
a[k]=temp;
sort(a+1,a+n,cmp);
}
void Graham(int n)
{
anglesort(n);
int i,head;
double sum=0;
a[n]=a[0];
stack1[0]=a[0];
stack1[1]=a[1];
stack1[2]=a[2];
head=2;
for(i=3;i<=n;i++)
{
while(head>=2 && (across(stack1[head-1],stack1[head],a[i])<=0)) head--;
stack1[++head]=a[i];
}
for(i=0;i<head;i++)
{
sum+=distan(stack1[i],stack1[i+1]);
}
printf("%.2lf\n",sum);
}
int main()
{
int i,n;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
if(n==1)
{
printf("0.00\n");
continue;
}
if(n==2)
{
printf("%.2lf\n",distan(a[0],a[1]));
continue;
}
Graham(n);
}
return 0;
}

25
HDOJ/1393_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<stdio.h>
int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
for(j=1;;j++)
{
if((n+m*n)%60==0)
{
printf("%d\n",j);
break;
}
n=(n+m*n)%60;
if(j>=1000)
{
printf("Impossible\n");
break;
}
}
}
return 0;
}

47
HDOJ/1394_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 5005
#define inf 999999999
int c[N];
int a[N];
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<N){
c[x]++;
x+=lowbit(x);
}
}
int get_sum(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
main()
{
int n, i, j, k;
while(scanf("%d",&n)==1){
memset(c,0,sizeof(c));
int num=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
a[i]++;
num+=get_sum(N-1)-get_sum(a[i]);
update(a[i]);
}
int minh=inf;
for(i=1;i<=n;i++)
{
num=num-(a[i]-1)+(n-a[i]);
minh=min(minh,num);
}
printf("%d\n",minh);
}
}

25
HDOJ/1395_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<iostream>
using namespace std;
int main()
{
int k,m,x,n;
while(cin>>n)
{
k=1;
x=2;
if(n%2==0||n==1)
cout<<"2^? mod "<<n<<" = 1"<<endl;
else
{
while(k++)
{
x*=2;
x=x%n;
if(x==1)
break;
}
cout<<"2^"<<k<<" mod "<<n<<" = 1"<<endl;
}
}
return 0;
}

20
HDOJ/1396_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<stdio.h>
#include<string.h>
int a[505];
int main()
{
int n,sum,i;
while(scanf("%d",&n)!=EOF)
{
for(i=n,sum=0;i>=1;i--)
{
sum=sum+(1+i)*i/2;
}
for(i=n-1;i>=0;i=i-2)
{
sum=sum+(1+i)*i/2;
}
printf("%d\n",sum);
}
return 0;
}

42
HDOJ/1397_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<iostream>
#define N 4000
#define MAX 32770
using namespace std;
int su[N],tot;
void prime()
{
tot=1;
su[0]=2;
for(int i=3;i<MAX;i+=2)
{
int ok=1;
for(int j=0;j<tot;j++)
{
if(su[j]*su[j]>i) break;
if(i%su[j]==0) {ok=0;break;}
}
if(ok) su[tot++]=i;
}
}
int main()
{
int n;
prime();
while(cin>>n&&n)
{
int num=0;
for(int i=0;2*su[i]<=n;i++)
{
int a=0,b=tot;
while(a<b)
{
int mid=(a+b)/2;
if(su[i]+su[mid]==n) {num++;break;}
if(su[i]+su[mid]<n) a=mid+1;
if(su[i]+su[mid]>n) b=mid;
}
}
cout<<num<<endl;
}
return 0;
}

27
HDOJ/1398_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<iostream>
using namespace std;
int main()
{
int a[350],b[350],i,j,k,n;
while(cin>>n&&n)
{
for(i=0;i<=n;i++)
{
a[i]=1;
b[i]=0;
}
for(i=2;i<=17;i++)
{
for(j=0;j<=n;j++)
for(k=0;k+j<=n;k+=i*i)
b[k+j]+=a[j];
for(j=0;j<=n;j++)
{
a[j]=b[j];
b[j]=0;
}
}
cout<<a[n]<<'\n';
}
return 0;
}

29
HDOJ/1399_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<stdio.h>
int cubic[54];
void init()
{
int i,j;
for(i=0,j=0;i<54;++i)
cubic[j++]=i*i*i;
}
int main()
{
int i,j,ans,sum;
init();
while(scanf("%d",&sum),sum)
{
ans=0;
for(i=0;i*(i+1)*(i+2)/6<=sum;++i)
{
for(j=53;j>=0;--j)
{
if(cubic[j]<=sum-i*(i+1)*(i+2)/6)
break;
}
if(ans<i*(i+1)*(i+2)/6+cubic[j])
ans=i*(i+1)*(i+2)/6+cubic[j];
}
printf("%d\n",ans);
}
return 0;
}