HDU 1004
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 115929 Accepted Submission(s): 45434
This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
該題需要一一對應,因此用的是map
已經AC過的代碼:
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
int n;
string a;
while(cin>>n&&n>0)
{
map<string, int>ss;
while(n--)
{
cin>>a;
ss[a]++;
}
int max=0;
string ans;
map<string, int>::iterator it;
for(it=ss.begin();it!=ss.end();it++)
{
if(it->second>max)
{
max=(*it).second;
ans=(*it).first;
}
}
cout<<ans<<endl;
}
return 0;
}
sdnuoj
1174.明明的隨機數
Description
Input
Output
Sample Input
10 20 40 32 67 40 20 89 300 400 15
Sample Output
8 15 20 32 40 67 89 300 400
set對於已經出現的數不會再重新記錄,而且set對於數據具有自動排序的功能
已經AC過的代碼:
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int main()
{
set<int>s;
s.clear();
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int m;
scanf("%d",&m);
s.insert(m);
}
int sum;
sum=s.size();
printf("%d\n",sum);
set<int >::iterator it1,it2;
it1=--s.end();
for(it2=s.begin();it2!=--s.end();it2++)
printf("%d ",*it2);
printf("%d\n",*it1);
return 0;
}
HDU1276
士兵隊列訓練問題
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7702 Accepted Submission(s): 3524
已經AC過的代碼:
方法一:用普通的數組:
#include<cstdio>
#include<cstring>
int main()
{
int t,n,k,i;
int a[5005],b[5005];
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
a[i]=i;
while(1)
{
if(n<=3)
{
for( i=1; i<n; i++)
printf("%d ",a[i]);
printf("%d\n",a[i]);
break;
}
k=0;
for(int i=1; i<=n; i++)
{
if(i%2!=0)
b[++k]=a[i];
}
if(k<=3)
{
for(int i=1; i<k; i++)
printf("%d ",b[i]);
printf("%d\n",b[k]);
break;
}
n=0;
for(int i=1; i<=k; i++)
{
if(i%3!=0)
a[++n]=b[i];
}
}
}
}
return 0;
}
方法二:用隊列完成的:
#include <iostream>
#include <queue>
using namespace std;
queue <int> s;
void cmp(int k)
{
int i = 1;
while(s.front() != 0)
{
if(i % k != 0) //用取模來實現數數的循環來數到相應的數字
s.push(s.front()); //把留下來的方隊尾
s.pop(); //去掉隊首
++i;
}
s.pop(); //把隊首的0去掉,在隊尾加上0
s.push(0);
}
int main()
{
int t, n;
cin >> t;
while(t-- && cin >>n)
{
for(int i = 1; i <=n; i++)
s.push(i);
s.push(0); //在最後面加個0代表隊尾的標誌
int i = 1;
while(s.size() > 4) //因爲加上了0,所以長度小於等於4就退出
{
if(i % 2 == 0) //選擇數三下踢一個人還是數兩下踢一個人
cmp(3); //i是偶數數則cmp(3),否則cmp(2)
else
cmp(2);
i++;
}
while(!s.empty())
{
if(s.front() != 0)
{
cout << s.front();
s.pop();
if(s.front() != 0)
cout << " ";
}
else
s.pop();//因爲隊尾還有個0,這個是不用輸出的但是爲了清空隊列,還是要pop他
}
cout << endl;
}
return 0;
}
方法三:使用鏈表的方式:
#include<iostream>
#include<list>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
list<int> s;
if(n==0) cout<<0<<endl;
else
{
for(int i=1; i<=n; i++)
s.push_back(i);
list<int>::iterator it,ip;//鏈表遍歷迭代器
int f=1;
int flag=s.size();
while(flag>3)
{
for(it=s.begin(); it!=s.end();)
{
if(f==2)
{
f=1;
ip=it;
it++;
s.erase(ip); //抹除操作
flag--;
}
else
{
f++;
it++;
}
}
f=1;
if(flag<=3) break;
for(it=s.begin(); it!=s.end();)
{
if(f==3)
{
f=1;
ip=it;
it++;
s.erase(ip);
flag--;
}
else
{
f++;
it++;
}
}
f=1;
if(flag<=3) break;
}
int w=0;
for(it=s.begin(); it!=s.end(); it++)
{
cout<<(*it); //注意輸出格式
w++;
if(w<=flag-1) cout<<' ';
}
cout<<endl;
}
}
return 0;
}
HDU 5831
Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1408 Accepted Submission(s): 631
Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".
Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different position i,j and swap S_i,S_j.
Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.
It is too difficult for Rikka. Can you help her?
For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.
這道題使用stack的方法更方便一點
已經AC過的代碼:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
int T,n;
char a[100010];
stack<char>s;
scanf("%d",&T);
while(T--)
{
int l=0,r=0,ans=0,wa=0,flag=1;
while(!s.empty())
s.pop();
scanf("%d",&n);
if(n)
scanf("%s",a);
if(n%2==1) printf("No\n");
else
{
int len=strlen(a);
for(int i=0; i<len; i++)
{
if(a[i]=='(')
{
l++;
s.push(a[i]);
}
else
{
r++;
if(!s.empty()&&s.top()=='(')
{
ans++;
s.pop();
}
else
{
wa++;
}
}
}
if(l==r)
{
if(s.empty())
{
if(ans!=1)printf("Yes\n");
else printf("No\n");
}
else if(wa==1)
printf("Yes\n");
else if(wa==2&&!s.empty())
printf("Yes\n");
else
printf("No\n");
}
else printf("No\n");
}
}
return 0;
}