Volume 1. String(uva)

10361 - Automatic Poetry

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream>

using namespace std;
int n;
char s1[105], s2[105];
int pr1, pr2, pl1, pl2;
int main()
{
    //ifstream cin("test.txt");
    int i;
    cin>>n;
    cin.get();
    while (n--)
    {
        cin.getline(s1, 105);
        cin.getline(s2, 105);
        for (i = 0; s1[i] != '<'; i++)
            cout<<s1[i];
        pl1 = i;
        for (i++; s1[i] != '>'; i++)
            cout<<s1[i];
        pr1 = i;
        for (i++; s1[i] != '<'; i++)
            cout<<s1[i];
        pl2 = i;
        for (i++; s1[i] != '>'; i++)
            cout<<s1[i];
        pr2 = i;
        for (i++; s1[i]; i++)
            cout<<s1[i];
        cout<<endl;
        for (i = 0; s2[i] != '.'; i++)
            cout<<s2[i];
        for (i = pl2 + 1; i < pr2; i++)
            cout<<s1[i];
        for (i = pr1 + 1; i < pl2; i++)
            cout<<s1[i];
        for (i = pl1 + 1; i < pr1; i++)
            cout<<s1[i];
        for (i = pr2 + 1; s1[i]; i++)
            cout<<s1[i];
        cout<<endl;

    }
    return 0;
}
View Code

537 - Artificial Intelligence?

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream>

using namespace std;
int n;
string s;
char res[5][100];
int main()
{
    //ifstream cin("test.txt");
    double U, I, P, scale;
    bool fu, fi, fp;
    cin>>n;
    int k = 1, i, j, p;
    cin.get();
    while (n--)
    {
        fu = fi = fp = false;
        p = 0;
        cout<<"Problem #"<<k++<<endl;
        getline(cin, s, '
');
        for (i = 0; s[i]; i++)
        {
            //if (i > 0 && s[i] == '=' &&(s[i - 1] == 'P' || s[i - 1] == 'I' || s[i - 1] == 'U'))
            if (s[i] == '=')
            {
                for (i--, j = 0; s[i] != 'V' && s[i] != 'A' && s[i] != 'W'; i++, j++)
                    res[p][j] = s[i];
                res[p][j] = '';
                p++;
            }
        }
        for (i = 0; i < 2; i++)
        {
            int len = strlen(res[i]) - 1;
            scale = 1;
            if (!isdigit(res[i][len]))
            {
                if (res[i][len] == 'k')
                    scale = 1000;
                else
                    if (res[i][len] == 'M')
                        scale = 1000000;
                    else
                        scale = 0.001;
                res[i][len] = '';
            }
            double tmp = atof(&res[i][2]) * scale;
            if (res[i][0] == 'I')
            {
                fi = true;
                I = tmp;
            }
            else
                if (res[i][0] == 'U')
                {
                    fu = true;
                    U = tmp;
                }
                else
                {
                    fp = true;
                    P = tmp;
                }
        }
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
        if (fu && fi)
        {
            cout<<"P="<<U * I<<"W"<<endl;
        }
        else
            if (fu && fp)
            {
                cout<<"I="<<P / U<<"A"<<endl;
            }
            else
            {
                cout<<"U="<<P / I<<"V"<<endl;
            }
            cout<<endl;
    }
    return 0;
}
View Code

409 - Excuses, Excuses!

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream>


using namespace std;
int k, e;
string words[20];
string ex[20];
int numex[20];

bool cmp(const string str)
{
    for (int i = 0; i < k; i++)
        if (str == words[i])
            return true;
    return false;
}
void calc()
{
    string tmp;
    int i, j, p, kmax = 0;
    memset(numex, 0, sizeof(numex));
    for (i = 0; i < e; i++)
    {
        j = 0;
        while (ex[i][j])
        {
            if (!isalpha(ex[i][j]))
            {
                j++;
                continue;
            }
            tmp.clear();
            while (isalpha(ex[i][j]))
            {
                tmp +=(ex[i][j++] | 32);
            }
            if (cmp(tmp))
            {
                numex[i]++;
            }
        }
        if (numex[i] > kmax)
            kmax = numex[i];
    }
    for (i = 0; i < e; i++)
        if (numex[i] == kmax)
            cout<<ex[i]<<endl;
    cout<<endl;
}
int main()
{
    ifstream cin("test.txt");
    int i, j, t = 1;
    while (cin>>k>>e)
    {
        cin.ignore();
        for (i = 0; i < k; i++)
            getline(cin, words[i]);
        for (i = 0; i < e; i++)
            getline(cin, ex[i]);
        cout<<"Excuse Set #"<<t++<<endl;
        calc();
    }
    return 0;
}
View Code

10878 - Decode the tape

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <fstream>
#include <cmath>

using namespace std;
string str;
int main()
{
    //ifstream cin("test.txt");
    int s, p, i;
    getline(cin, str);
    while (getline(cin, str), str[0] != '_')
    {
        s = p = 0;
        for (i = 9; i > 1; i--)
        {
            if (str[i] == '.')
                continue;
            if (str[i] == 'o')
                s += pow(2, p);
            p++;
        }
        cout<<(char)s;
    }
    return 0;
}
View Code

10815 - Andy's First Dictionary

#include <iostream>
#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
class cmp
{
public:
    bool operator()(const string s1, const string s2)
    {
        return s1 < s2;
    }
};


set<string, cmp> s;
int main()
{
    freopen("test.txt", "r", stdin);
    string tmp;
    char ch = 1;
    while (ch != EOF)
    {
        ch = getchar();
        if (!isalpha(ch))
            continue;
        tmp.clear();
        while (true)
        {
            tmp += (ch | 32);
            ch = getchar();
            if (!isalpha(ch))
                break;
        }
        s.insert(tmp);
    }
    for (set<string>::iterator it = s.begin(); it != s.end(); it++)
        cout<<*it<<endl;
    return 0;
}
View Code

644 - Immediate Decodability

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
string set[10];
bool cmp(const string s1, const string s2)
{
    int i = 0;
    while (s1[i] && s2[i])
    {
        if (s1[i] != s2[i])
            break;
        i++;
    }
    if (!s1[i] || !s2[i])
        return true;
    return false;
}
int main()
{
    //ifstream cin("test.in");
    int n, i, j, t = 1;
    while (getline(cin, set[0]))
    {
        cout<<"Set "<<t++<<" is ";
        n = 1;
        while (getline(cin, set[n]), set[n][0] != '9')
            n++;
        for (i = 0; i < n - 1; i++)
        {
            for (j = i + 1; j < n; j++)
            {
                if (cmp(set[i], set[j]))
                    break;
            }
            if (j < n)
                break;
        }
        if (i < n - 1)
            cout<<"not ";
        cout<<"immediately decodable"<<endl;
    }
    return 0;
}
View Code

10115 - Automatic Editing

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define N 15
using namespace std;
string f[N], re[N];
string edit;
int n, q;
bool cmp(int p)
{
    int i, j, k, m;
    for (i = q; i < n; i++)
    {
        for (m = p; edit[m]; m++)
        {
            for (j = 0, k = m; f[i][j] && edit[k]; j++, k++)
            {
                if (edit[k] != f[i][j])
                    break;
            }
            if (!f[i][j])//替换
            {
                edit.replace(edit.begin() + m, edit.begin() + k, re[i]);
                if (i != q)
                    q = i;
                return true;
            }
        }

    }
    return false;
}
int main()
{
    int i, j;
    while (cin>>n, n)
    {
        cin.get();
        q = 0;
        for (i = 0; i < n; i++)
        {
            getline(cin, f[i]);
            getline(cin, re[i]);
        }
        getline(cin, edit);
        for (i = 0; edit[i]; i++)
        {
            if (cmp(i))
                i = -1;
        }
        cout<<edit<<endl;
    }
}
View Code
原文地址:https://www.cnblogs.com/jecyhw/p/3675470.html