经典算法题题解

利用栈解决hdu 1062“Text Reverse”问题:
#include<cstdio>
#include<stack>
using namespace std;
int main() {
    int n;
    char ch;
    scanf_s("%d", &n);
    getchar();
    while (n--) {
        stack<int>s;
        while (true) {
            ch = getchar();
            if (ch == ' ' || ch == '\n' || ch == EOF) {
                while (!s.empty()) {
                    printf("%c", s.top());
                    s.pop();
                }
                if (ch == '\n' || ch == EOF) break;
                printf(" ");
            }
            else  s.push(ch);
        }
        printf("\n");
    }
    return 0;
}
栈和队列解决hdu 1702“ACboy needs your help again”
#include<iostream>
#include<string>
#include<queue>
#include<stack>
using namespace std;
int main() {
    int t, n, temp;
    cin >> t;
    while (t--) {
        string str1, str2;
        queue<int>Q;
        stack<int>S;
        cin >> n >> str1;
        for (int i = 0; i < n; i++) {
            if (str1 == "FIFO") {
                cin >> str2;
                if (str2 == "IN") {
                    cin >> temp;
                    Q.push(temp);
                }
                if (str2 == "OUT") {
                    if (Q.empty()) cout << "None" << endl;
                    else {
                        cout << Q.front() << endl;
                        Q.pop();
                    }
                }
            }
            else {
                cin >> str2;
                if (str2 == "IN") {
                    cin >> temp;
                    S.push(temp);
                }
                if (str2 == "OUT") {
                    if (S.empty())
                        cout << "None" << endl;
                    else {
                        cout << S.top() << endl;
                        S.pop();
                    }
                }
            }
        }
    }
    return 0;
}
List解决hdu 1276“士兵队列训练问题”:
#include<iostream>
#include<list>
#include<iterator>
using namespace std;
int main() {
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        int k = 2;
        list<int>mylist;
        list<int>::iterator it;
        for (int i = 0; i <= n; i++)
            mylist.push_back(i);
        while (mylist.size() > 3) {
            int num = 1;
            for (it = mylist.begin(); it != mylist.end();) {
                if (num++ % k == 0)
                    it = mylist.erase(it);
                else
                    it++;
            }
            k == 2 ? k = 3 : k = 2;
        }
        for (it = mylist.begin(); it != mylist.end(); it++) {
            if (it != mylist.begin())
                cout << " ";
            cout << *it;
        }
        cout << endl;

        return 0;

    }
}
Set解决hdu2094“产生冠军”
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main() {
    set<string>A, B;
    string s1, s2;
    int n;
    while (cin >> n && n) {
        for (int i = 0; i < n; i++) {
            cin >> s1 >> s2;
            A.insert(s1);
            A.insert(s2);
            B.insert(s2);
        }
        if (A.size() - B.size() == 1)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
        A.clear();
        B.clear();
    }
    return 0;
}
Map解决hdu2648“shopping”问题:
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main() {
	int n, m, p;
	map<string, int>shop;
	while (cin >> n) {
		string s;
		for (int i = 1; i < n; i++)cin >> s;
		cin >> m;
		while (m--) {
			for (int i = 1; i < n; i++) {
				cin >> p >> s;
				shop[s] += p;
			}
			int rank = 1;
			map<string, int>::iterator it;
			for (it = shop.begin(); it != shop.end(); it++)
				if (it->second > shop["memory"])
					rank++;
			cout << rank << endl;
		}
		shop.clear();
	}
	return 0;
}
动态规划—DP转移矩阵求解hdu2069“Coin Change”
#include<iostream>
using namespace std;
const int COIN=101;
const int MONEY= 251;
int dp[MONEY][COIN]={0};
int type[5]={1,5,10,25,30};
void solve(){
    dp[0][0]=1;
    for(int i=0;i<5;i++)
        for(int j=1;j<COIN;j++)
            for(int k=type[i];k<MONEY;k++)
                dp[k][j]+=dp[k-type[i]][j-1];
}
int main(){
    int s;
    int ans[MONEY]={0};
    solve();
    for(int i=0;i<MONEY;i++)
        for(int j=0;j<COIN;j++)
            ans[i]+=dp[i][j];
    while(cin>>s)
        cout<<ans[s]<<endl;
    return 0;
}
动态规划—DP转移矩阵求解hdu2602“Bone Collector”
#include<iostream>
using namespace std;
struct BONE {
    int val;
    int vol;
}bone[1011];
int T, N, V;
int dp[1011][1011];
int ans() {
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= N; i++)
        for (int j = 0; j <= V; j++) {
            if (bone[i].vol > j)
                dp[i][j] = dp[i - 1][j];
            else
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - bone[i].vol] + bone[i].val);
        }
    return dp[N][V];
}
int main() {
    cin >> T;
    while (T--) {
        cin >> N >> V;
        for (int i = 1; i <= N; i++)cin >> bone[i].val;
        for (int i = 1; i <= N; i++)cin >> bone[i].vol;
        cout << ans() << endl;
    }
    return 0;
}
动态规划—DP转移矩阵求解hdu1159“ Common Sqbquence”
#include<iostream>
#include<string>
using namespace std;
int dp[1005][1005];
string str1, str2;
int LCS() {
	memset(dp, 0, sizeof(dp));
	for(int i=1;i<=str1.length();i++)
		for (int j = 1; j <= str2.length(); j++) {
			if (str1[i - 1] == str2[j - 1])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
		}
	return dp[str1.length()][str2.length()];
}
int main() {
	while (cin >> str1 >> str2)
		cout << LCS() << endl;
	return 0;
}
动态规划—DP转移矩阵求解hdu1257“最少拦截系统”
#include<iostream>
using namespace std;
const int MAXN = 10000;
int n, high[MAXN];
int LIS() {
	int ans = 1;
	int dp[MAXN];
	dp[1] = 1;
	for (int i = 2; i <= n; i++) {
		int max = 0;
		for (int j = 1; j < i; j++)
			if (dp[j] > max && high[j] < high[i])
				max = dp[j];
		dp[i] = max + 1;
		if (dp[i] > ans)ans = dp[i];
	}
	return ans;
}
int main() {
	while (cin >> n) {
		for (int i = 1; i <= n; i++)
			cin >> high[i];
		cout << LIS() << endl;
	}
	return 0;
}
Poj3981“字符串替换”问题题解 :
#include<iostream>
#include<string>
using namespace std;
int main() {
	string str;
	int pos;
	while (getline(cin, str)) {
		while ((pos = str.find("you")) != -1)
			str.replace(pos, 3, "we");
		cout << str << endl;
	}
	return 0;
}

Hdu2648的字符串哈希程序:

#include<iostream>
#include<vector>
using namespace std;
const int N = 10005;
struct node {
    char name[35];
    int price;
};
vector<node>List[N];
unsigned int BKDRHash(char* str) {
    unsigned int seed = 31, key = 0;
    while (*str)
        key = key * seed + (*str++);
    return key & 0x7fffffff;
}
int main() {
    int n, m, key, add, memory_price, rank, len;
    int p[N];
    char s[35];
    node t;
    while (cin >> n) {
        for (int i = 0; i < N; i++)
            List[i].clear();
        for (int i = 0; i < n; i++) {
            cin >> t.name;
            key = BKDRHash(t.name) % N;
            List[key].push_back(t);
        }
        cin >> m;
        while (m--) {
            rank = len = 0;
            for (int i = 0; i < n; i++) {
                cin >> add >> s;
                key = BKDRHash(s) % N;
                int t= (List[key].size());
                for (int j = 0; j < t; j++)
                    if (strcmp(List[key][j].name, s) == 0) {
                        List[key][j].price += add;
                        if (strcmp(s, "memory") == 0)
                            memory_price = List[key][j].price;
                        else
                            p[len++] = List[key][j].price;
                        break;
                    }
            }
            for (int i = 0; i < len; i++)
                if (memory_price < p[i])
                    rank++;
            cout << rank + 1 << endl;
        }
    }
    return 0;
}
Hdu1251“字典树”问题题解
法一:map实现:
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main() {
    char str[10];
    map<string, int>m;
    while (gets_s(str)) {
        int len = strlen(str);
        if (!len) break;
        for (int i = len; i > 0; i--) {
            str[i] = '\0';
            m[str]++;
        }
    }
    while (gets_s(str))
        cout << m[str] << endl;
    return 0;
}
法二:使用数组实现字典树:
#include<iostream>
using namespace std;
int trie[1000010][26];
int num[1000010] = { 0 };
int pos = 1;
void Insert(char str[]) {
    int p = 0;
    for (int i = 0; str[i]; i++) {
        int n = str[i] - 'a';
        if (trie[p][n] == 0)
            trie[p][n] = pos++;
        p = trie[p][n];
        num[p]++;
    }
}
int Find(char str[]) {
    int p = 0;
    for (int i = 0; str[i]; i++) {
        int n = str[i] - 'a';
        if (trie[p][n] == 0)
            return 0;
        p = trie[p][n];
    }
    return num[p];
}
int main() {
    char str[11];
    while (gets_s(str)) {
        if (!strlen(str))    break;
        Insert(str);
    }
    while (gets_s(str))
        cout << Find(str) << endl;
    return 0;
}
采用KMP算法解决hdu2087“剪花布条”
#include<iostream>
using namespace std;
const int MAXN = 1000 + 5;
char str[MAXN], pattern[MAXN];
int Next[MAXN];
int cnt;
int getFail(char* p, int plen) {
    Next[0] = 0;
    Next[1] = 0;
    for (int i = 1; i < plen; i++) {
        int j = Next[i];
        while (j && p[i] != p[j])
            j = Next[j];
        Next[i + 1] = (p[i] == p[j]) ? j + 1 : 0;
    }
}
int kmp(char* s, char* p) {
    int last = -1;
    int slen = strlen(s), plen = strlen(p);
    getFail(p, plen);
    int j = 0;
    for (int i = 0; i < slen; i++) {
        while (j && s[i] != p[j])
            j = Next[j];
        if (s[i] == p[j])
            j++;
        if (j == plen) {
            //printf("at location=%d,%s\n",i+1-plen,&s[i+1-plen]);
            if (i - last >= plen) {
                cnt++;
                last = i;
            }
        }
    }
}
int main() {
    while (~scanf("%s", str)) {
        if (str[0] == '#')
            break;
        scanf("%s", pattern);
        cnt = 0;
        kmp(str, pattern);
        printf("%d\n", cnt);
    }
    return 0;
}
Hdu5578”Friend of Frog”题解:
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100010;
char s[N];
void solve() {
    scanf("%s", s);
    int n = strlen(s);
    int ans = -1;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= 26 && i - j >= 0; ++j) {
            if (s[i] == s[i - j]) {
                if (ans == -1 || j < ans) {
                    ans = j;
                }
                break;
            }
        }
    }
    printf("%d\n", ans);
}
int main() {
    int t;
    scanf("%d", &t);
    for (int i = 1; i <= t; ++i) {
        printf("Case # %d:", i);
        solve();
    }
    return 0;
}
Hdu5584“LCM Walk”
#include<iostream>
#include<algorithm>
using namespace std;
int solve(long long ex, long long ey) {
    int ans = 1;
    long long t,p,q;
    while (true) {
        if (ex > ey)
        p=ex/t;q=ey/(ex+t);
        if ((ey % (ex + t)) == 0) {
            ey = ey * t / (ex + t);
            ans++;
        }
        else
            break;
    }
    return ans;
}
int main() {
    int T;
    long long ex, ey;
    scanf_s("%d", &T);
    for (int cas = 1; cas <= T; cas++) {
        scanf_s("%lld%lld", &ex, &ey);
        printf("Case # %d:%d\n", cas, solve(ex, ey));
    }
    return 0;
}
(3n+1)猜想问题题解
#include<cstdio>
int main(){
    int n,step=0;
    scanf("%d",&n);
    while(n!=1){
        if(n%2==0)n=n/2;
        else n=(3*n+1)/2;
        step++;
    }
    printf("%d\n",step);
    return 0;
}
A+B和C问题题解:
#include<cstdio>
int main(){
    int T,tcase=1;
    scanf("%d",&T);
    while(T--){
        long long a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        if(a+b>c){
            printf("Case #%d: true\n",tcase++);
        }else{
            printf("Case #%d: false\n",tcase++);
        }
    }
    return 0;
}
部分A+B问题题解
#include<cstdio>
int main(){
    long long a,b,da,db;
    scanf("%lld%lld%lld%lld",&a,&b,&da,&db);
    long long pa=0,pb=0;
    while(a!=0){
        if(a%10==da)
            pa=pa*10+da;
        a=a/10;
    }
    while(b!=0){
        if(b%10==db)
            pb=pb*10+db;
        b=b/10;
    }
    printf("%lld\n",pa+pb);
    return 0;
}
程序运行时间问题题解
#include<cstdio>
int main(){
    int C1,C2;
    scanf("%d%d",&C1,&C2);
    int ans=C2-C1;
    if(ans%100>=50){
        ans=ans/100+1;
    }else{
        ans=ans/100;
    }
    printf("%02d:%02d:%02d\n",ans/3600,ans%3600/60,ans%60);
    return 0;
}
划拳问题题解
#include<cstdio>
int main(){
    int n,failA=0,failB=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        int a1,a2,b1,b2;
        scanf("%d%d%d%d",&a1,&a2,&b1,&b2);
        if(a1+b1==a2&&a1+b1!=b2){
            failB++;
        }else if (a1+b1!=a2&&a1+b1==b2)
        {
            failA++
        }
    }
    printf("%d %d\n",failA,failB);
    return 0;
}
数组元素循环右移问题题解
#include<cstdio>
int main(){
    int a[110];
    int n,m,count=0;
    scanf("%d%d",&n,&m);
    m=m%n;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=n-m;i<n;i++){
        printf("%d",a[i]);
        count++;
        if(count<n) printf(" ");
    }
    for(int i=0;i<n-m;i++){
        printf("%d",a[i]);
        count++;
        if(count<n) printf(" ");
    }
    return 0;
}

数字分类问题题解:
#include<cstdio>
int main(){
    int count[5]={0};
    int ans[5]={0};
    int n,temp;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&temp);
        if(temp%5==0){
            if(temp%2==0){
                ans[0]+=temp;
                count[0]++;
            }
        }else if (temp%5==1){
            if(count[1]%2==0){
                ans[1]+=temp;
            }else{
                ans[1]-=temp;
            }
            count[1]++;
        }else if(temp%5==2){
            count[2]++;
        }else if(temp%5==3){
            ans[3]+=temp;
            count[3]++;
        }else{
            if(temp>ans[4]){
                ans[4]=temp;
            }
            count[4]++;
        }
    }
    if(count[0]==0) printf("N");
    else printf("%d",ans[0]);
    if(count[1]==0) printf("N");
    else printf("%d",ans[1]);
    if(count[2]==0) printf("N");
    else printf("%d",ans[2]);
    if(count[3]==0) printf("N");
    else printf("%.lf",(double)ans[3]/count[3]);
    if(count[4]==0) print("N");
    else printf("%d",ans[4]);
    return 0;
}
石头剪刀布问题题解:
#include<cstdio>
int change(char c){
    if(c=='B')return 0;
    if(c=='C')return 1;
    if(c=='J')return 2;
}
int main(){
    char mp[3]={'B','C','J'};
    int n;
    scanf("%d",&n);
    int times_A[3]={0},times_B[3]={0};
    int hand_A[3]={0},hand_B[3]={0};
    char c1,c2;
    int k1,k2;
    for(int i=0;i<n;i++){
        getchar();
        scanf("%c %c",&c1,&c2);
        k1=change(c1);
        k2=change(c2);
        if((k1+1)%3==k2){
            times_A[0]++;
            times_B[2]++;
            hand_A[k1]++;
        }else if(k1==k2){
            times_A[1]++;
            times_B[1]++;
        }else{
            times_A[2]++;
            times_B[0]++;
            hand_B[k2]++;
        }
    }
    printf("%d %d %d\n",times_A[0],times_A[1],times_A[2]);
    printf("%d %d %d\n",times_B[0],times_B[1],times_B[2]);
    int id1=0,id2=0;
    for(int i=0;i<3;i++){
        if(hand_A[i]>hand_A[id1])id1=i;
        if(hand_B[i]>hand_B[id2])id2=i;
    }
    printf("%c %c\n",mp[id1],mp[id2]);
    return 0;
}
Shuffling Machine问题题解
#include<cstdio>
const int N=54;
char mp[5]={'S','H','C','D','J'};
int start[N+1],end[N+1],next[N+1];
int main(){
    int K;
    scanf("%d",&K);
    for(int i=1;i<=N;i++){
        start[i]=1;
    }
    for(int i=1;i<=N;i++){
        scanf("%d",&next[i]);
    }
    for(int step=0;step<K;step++){
        for(int i=1;i<=N;i++){
            end[next[i]]=start[i];
        }
        for(int i=1;i<=N;i++){
            start[i]=end[i];
        }
    }
    for(int i=1;i<=N;i++){
        if(i!=1)printf(" ");
        start[i]--;
        printf("%c%d",mp[start[i]/13],start[i]%13+1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超翔之逸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值