Tuesday, December 29, 2015

【UVa】10340 – All in All

題目內容

Time limit: 3.000 seconds
You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string. Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.

Input

The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace. Input is terminated by EOF.

Output

For each test case output, if s is a subsequence of t.

Sample Input

sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

Sample Output

Yes
No
Yes
No

Solution

#include <iostream>
#include <string>
#include <stdio.h>
 
using namespace std;
 
int main(){
    string s, t;
    while(cin >> s >> t){
        int tpointer = 0;
        int spointer = 0;
        int count = 0;
        while(spointer < s.length() && tpointer < t.length()){
            if(s[spointer] == t[tpointer]){
                count++;
                spointer++;
                tpointer++;
            }else{
                tpointer++;
            }
        }
         
        if(count == s.length()){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
     
    return 0;
}

【UVa】1225 – Digit Counting

題目內容

Time limit: 3.000 seconds
Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 to N (1 < N < 10000) . After that, he counts the number of times each digit (0 to 9) appears in the sequence. For example, with N = 13 , the sequence is:12345678910111213In this sequence, 0 appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and each digit from 4 to 9 appears once. After playing for a while, Trung gets bored again. He now wants to write a program to do this for him. Your task is to help him with writing this program.

Input 

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each test case, there is one single line containing the number N .

Output 

For each test case, write sequentially in one line the number of digit 0, 1,…9 separated by a space.

Sample Input 

2 
3 
13

Sample Output 

0 1 1 1 0 0 0 0 0 0 
1 6 2 2 1 1 1 1 1 1

Solution

總之就是把數字用字元串來處理,之後int ia = str[j] – ‘0’;把字元串轉回Integer,然後輸出就可以了


#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
 
using namespace std;
 
int zero, one, two, three, four, five, six, seven, eight, nine;
void addNum(int num){
    switch(num){
        case 0:
            zero++;
            break;
        case 1:
            one++;
            break;
        case 2:
            two++;
            break;
        case 3:
            three++;
            break;
        case 4:
            four++;
            break;
        case 5:
            five++;
            break;
        case 6:
            six++;
            break;
        case 7:
            seven++;
            break;
        case 8:
            eight++;
            break;
        case 9:
            nine++;
            break;
    }
}
 
int main(){
    int t;
    while(cin >> t){
        while(t--){
            zero = 0;one = 0; two = 0; three = 0; four = 0; five = 0; six = 0; seven = 0; eight = 0; nine = 0;
            int n;
            vector<int> vn;
            cin >> n;
            for(int i = 1; i <= n; i++){
                vn.push_back(i);
            }
             
 
            for(int i = 0; i < vn.size(); i++){
                string str;               
                stringstream ss;
                ss << vn[i]; ss >> str;
                for(int j = 0; j < str.length(); j++){
                    int ia = str[j] - '0';
                    addNum(ia);
                }
             
            }
             
            cout << zero << " " << one << " " << two << " " << three << " " << four << " " << five << " " << six << " " << seven << " " << eight << " " << nine << endl;
             
        }
    }
     
    return 0;
}